echo "[{"Name": "bar"}]" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj[0]["Name"]);')

Python extract json


0
By: moerin
2018-03-31 21:31:53

These Might Interest You

  • Validates and pretty-prints the content fetched from the URL. Show Sample Output


    11
    curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R
    keimlink · 2010-03-24 09:15:12 1
  • If you tried the other Python version of Convert JSON to YAML and you end up with lines that has "!!python/unicode", this version of the command is for you.


    1
    python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, allow_unicode=True)' < foo.json > foo.yaml
    renoirb · 2015-01-20 15:58:29 0
  • You can use a site like http://www.jsonlint.com/ or use the command line to validate your long and complex json data. This is part of the simplejson package for python http://undefined.org/python/#simplejson. Wrong json expression example: echo '{ 1.2:3.4}' | python -m simplejson.tool Expecting property name: line 1 column 2 (char 2) Show Sample Output


    12
    echo '{"json":"obj"}' | python -m simplejson.tool
    azeey · 2009-05-10 13:58:05 4
  • You have to do that before : cd ~/.mozilla/firefox/<YOUR PROFILE> Require bash. If you use something else, you may use echo <CODE> | python Forked from ArkSeth python script.


    3
    python2 <<< $'import json\nf = open("sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'
    sputnick · 2009-12-28 21:56:05 0
  • Converts YAML file to JSON. Note that you'll need to install PyYAML. Also some YAML data types (like dates) are not supported by JSON).


    4
    python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < file.yaml > file.json
    tebeka · 2013-04-24 00:28:55 0
  • For situations where you keep JSON in a VCS and you want your diffs to be sane, such as within a Chef configuration repo.


    1
    cat foo.json | python -mjson.tool
    jeffgage · 2013-05-06 20:11:05 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: