终端:CURL | 走到JsonPath | prettyprint JSON

chr*_*zer 6 shell json

我正在寻找在shell上打印返回的JSON文档的一部分的可能性.

现在,我正在将来自cURL的HTTP响应传递给python以进行相同的打印:

curl -vX GET http://foo.bar | python -mjson.tool
Run Code Online (Sandbox Code Playgroud)

但是现在我想知道我怎么能"走"到一个子阵列的路径?

例如,如果是返回的JSON数据:

{
"value1": true,
"value2": {
            "subvalue1": "foo",
            "subvalue2": "bar"
          }
}
Run Code Online (Sandbox Code Playgroud)

我怎么才能在这个例子中打印子阵列?

Eri*_*del 2

几个月前,特里·琼斯(Terry Jones)编写了一个很好的工具来完成此任务。这是他关于它的博客文章http://blogs.fluidinfo.com/terry/2010/11/25/jsongrep-py-python-for-extracting-pieces-of-json-objects/

基本上,对于您的示例,您将运行

curl -vX GET http://foo.bar | jsongrep.py value2
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我不认为你的例子实际上是有效的 JSON。子数组(实际上是 JSON 术语中的子对象)应该是

{
   "subvalue1": "foo",
   "subvalue2": "bar"
}
Run Code Online (Sandbox Code Playgroud)