python outputs the instance code in the data json format
- 2020-05-12 02:52:05
- OfStack
There is a requirement to display json data on standard output of python, and it would be nice to indent it to see the data. Packages that use json have a lot to do here
import json
date = {u'versions': [{u'status': u'CURRENT', u'id': u'v2.3', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.2', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.1', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.0', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.1', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.0', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}]}
print json.dumps(data, sort_keys=True, indent=2) # Sort and indent the two-character output
This results in the following output:
{
"versions": [
{
"id": "v2.3",
"links": [
{
"href": "http://controller:9292/v2/",
"rel": "self"
}
],
"status": "CURRENT"
},
{
"id": "v2.2",
"links": [
{
"href": "http://controller:9292/v2/",
"rel": "self"
}
],
"status": "SUPPORTED"
},
{
"id": "v2.1",
"links": [
{
"href": "http://controller:9292/v2/",
"rel": "self"
}
],
"status": "SUPPORTED"
},
{
"id": "v2.0",
"links": [
{
"href": "http://controller:9292/v2/",
"rel": "self"
}
],
"status": "SUPPORTED"
},
{
"id": "v1.1",
"links": [
{
"href": "http://controller:9292/v1/",
"rel": "self"
}
],
"status": "SUPPORTED"
},
{
"id": "v1.0",
"links": [
{
"href": "http://controller:9292/v1/",
"rel": "self"
}
],
"status": "SUPPORTED"
}
]
}
You can see it's all formatted.
This is in python. If you use the command line directly and want to convert directly, you can use data | python-mjson.tool to output data in json format
echo '{"first_key": "value", "second_key": "value2"}' | python -mjson.tool
For example, if you want to filter the value of first_key directly from the command line, you can:
echo '{"first_key": "value", "second_key": "value2"}' | python -c 'import sys, json; print json.load(sys.stdin)[sys.argv[1]]' first_key
So you get value of theta.