Python _python The instance code of the data JSON format output

Source: Internet
Author: User

There is a requirement that the JSON format data be displayed in the standard output of Python, and if the indentation display looks good on the data, there are many things to do with the JSON package

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) # sorted and indented two characters output

This will produce 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 "
  }
 ]
}

As you can see, it's all formatted.

This is in Python, if you use the command line directly, you want to convert directly, you can use data | Python-mjson.tool to output JSON-formatted data

Echo ' {' First_key ': ' Value ', ' second_key ': ' value2 '} ' | Python-mjson.tool

For example, to filter the value of First_key to the command line directly, you can:

Echo ' {' First_key ': ' Value ', ' second_key ': ' value2 '} ' | Python-c ' Import sys, JSON; Print Json.load (Sys.stdin) [sys.argv[1]] ' First_key

You'll get the value for that.

The above is a small series for everyone to bring the data JSON format of the output of the example code all content, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.