6

I am looking for a possibility to print parts of a returned JSON document on the shell.

Right now, I am piping a HTTP response from cURL to python to prettyprint it:

curl -vX GET http://foo.bar | python -mjson.tool

But now I would like to know how I could actually "walk" the path to a subarray?

If was for example the returned JSON data:

{
"value1": true,
"value2": {
            "subvalue1": "foo",
            "subvalue2": "bar"
          }
}

How could I only print the subarray in this example?

1 Answer 1

2

Terry Jones wrote a nice tool for doing exactly this a few months ago. Here's his blog post about it http://blogs.fluidinfo.com/terry/2010/11/25/jsongrep-py-python-for-extracting-pieces-of-json-objects/.

Basically, for your example you would run

curl -vX GET http://foo.bar | jsongrep.py value2

By the way, I don't think your example is actually valid JSON. The subarray (actually a sub-object in JSON terms) should just be

{
   "subvalue1": "foo",
   "subvalue2": "bar"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.