【问题标题】:Parse JSON data using jq and curl from command line从命令行使用 jq 和 curl 解析 JSON 数据
【发布时间】:2019-12-21 18:08:21
【问题描述】:

我想从命令行使用 jq 和 curl 解析 JSON 数据,但我没有成功。事实上,我想得到 response > textAnnotations > description 的值,但我做错了。这是我的命令:

curl -X POST > response.txt \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
      'requests': [
        {
          'image': {
            'source': {
              'imageUri': 'http://adn-cdn2.avadeo.net/articles/vignettes/grandes/146.jpg'
            }
          },
          'features': [
            {
              'type': 'TEXT_DETECTION'
            }
          ]
        }
      ]
    }" "https://vision.googleapis.com/v1/images:annotate?key=XXXXXXXXXXXXXXXXXXX" | jq -r '.responses.textAnnotations.description'

如果我不使用 JQ 过滤,这是 Curl 的答案:

    {
  "responses": [
    {
      "textAnnotations": [
        {
          "locale": "und",
          "description": "24\nmois\n12\nmois\n",
          "boundingPoly": {
            "vertices": [
              {
                "x": 12,
                "y": 24
              },
              {
                "x": 285,
                "y": 24
              },
              {
                "x": 285,
                "y": 206
              },
              {
                "x": 12,
                "y": 206
              }
            ]
          }
        },
        {
          "description": "24",
          "boundingPoly": {
            "vertices": [
              {
                "x": 216,
                "y": 24
              },
              {
                "x": 285,
                "y": 24
              },
              {
                "x": 285,
                "y": 83
              },
              {
                "x": 216,
                "y": 83
              }
            ]
          }
        },
        {
          "description": "mois",
          "boundingPoly": {
            "vertices": [
              {
                "x": 223,
                "y": 83
              },
              {
                "x": 280,
                "y": 84
              },
              {
                "x": 280,
                "y": 110
              },
              {
                "x": 223,
                "y": 109
              }
            ]
          }
        },
        {
          "description": "12",
          "boundingPoly": {
            "vertices": [
              {
                "x": 12,
                "y": 122
              },
              {
                "x": 81,
                "y": 122
              },
              {
                "x": 81,
                "y": 179
              },
              {
                "x": 12,
                "y": 179
              }
            ]
          }
        },
        {
          "description": "mois",
          "boundingPoly": {
            "vertices": [
              {
                "x": 19,
                "y": 177
              },
              {
                "x": 76,
                "y": 176
              },
              {
                "x": 76,
                "y": 205
              },
              {
                "x": 19,
                "y": 206
              }
            ]
          }
        }
      ],
      "fullTextAnnotation": {
        "pages": [
          {
            "width": 300,
            "height": 225,
            "blocks": [
              {
                "boundingBox": {
                  "vertices": [
                    {
                      "x": 216,
                      "y": 24
                    },
                    {
                      "x": 285,
                      "y": 24
                    },
                    {
                      "x": 285,
                      "y": 83
                    },
                    {
                      "x": 216,
                      "y": 83
                    }
                  ]
                },
                "paragraphs": [
                  {
                    "boundingBox": {
                      "vertices": [
                        {
                          "x": 216,
                          "y": 24
                        },
                        {
                          "x": 285,
                          "y": 24
                        },
                        {
                          "x": 285,
                          "y": 83
                        },
                        {
                          "x": 216,
                          "y": 83
                        }
                      ]
                    },
                    "words": [
                      {
                        "boundingBox": {
                          "vertices": [
                            {
                              "x": 216,
                              "y": 24
                            },
                            {
                              "x": 285,
                              "y": 24
                            },
                            {
                              "x": 285,
                              "y": 83
                            },
                            {
                              "x": 216,
                              "y": 83
                            }
                          ]
                        },
                        "symbols": [
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 216,
                                  "y": 25
                                },
                                {
                                  "x": 248,
                                  "y": 25
                                },
                                {
                                  "x": 248,
                                  "y": 76
                                },
                                {
                                  "x": 216,
                                  "y": 76
                                }
                              ]
                            },
                            "text": "2"
                          },
                          {
                            "property": {
                              "detectedBreak": {
                                "type": "EOL_SURE_SPACE"
                              }
                            },
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 255,
                                  "y": 24
                                },
                                {
                                  "x": 285,
                                  "y": 24
                                },
                                {
                                  "x": 285,
                                  "y": 83
                                },
                                {
                                  "x": 255,
                                  "y": 83
                                }
                              ]
                            },
                            "text": "4"
                          }
                        ]
                      }
                    ]
                  }
                ],
                "blockType": "TEXT"
              },
              {
                "boundingBox": {
                  "vertices": [
                    {
                      "x": 223,
                      "y": 83
                    },
                    {
                      "x": 280,
                      "y": 84
                    },
                    {
                      "x": 280,
                      "y": 110
                    },
                    {
                      "x": 223,
                      "y": 109
                    }
                  ]
                },
                "paragraphs": [
                  {
                    "boundingBox": {
                      "vertices": [
                        {
                          "x": 223,
                          "y": 83
                        },
                        {
                          "x": 280,
                          "y": 84
                        },
                        {
                          "x": 280,
                          "y": 110
                        },
                        {
                          "x": 223,
                          "y": 109
                        }
                      ]
                    },
                    "words": [
                      {
                        "boundingBox": {
                          "vertices": [
                            {
                              "x": 223,
                              "y": 83
                            },
                            {
                              "x": 280,
                              "y": 84
                            },
                            {
                              "x": 280,
                              "y": 110
                            },
                            {
                              "x": 223,
                              "y": 109
                            }
                          ]
                        },
                        "symbols": [
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 223,
                                  "y": 89
                                },
                                {
                                  "x": 242,
                                  "y": 89
                                },
                                {
                                  "x": 242,
                                  "y": 108
                                },
                                {
                                  "x": 223,
                                  "y": 108
                                }
                              ]
                            },
                            "text": "m"
                          },
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 246,
                                  "y": 88
                                },
                                {
                                  "x": 259,
                                  "y": 88
                                },
                                {
                                  "x": 259,
                                  "y": 108
                                },
                                {
                                  "x": 246,
                                  "y": 108
                                }
                              ]
                            },
                            "text": "o"
                          },
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 262,
                                  "y": 83
                                },
                                {
                                  "x": 264,
                                  "y": 83
                                },
                                {
                                  "x": 264,
                                  "y": 109
                                },
                                {
                                  "x": 262,
                                  "y": 109
                                }
                              ]
                            },
                            "text": "i"
                          },
                          {
                            "property": {
                              "detectedBreak": {
                                "type": "EOL_SURE_SPACE"
                              }
                            },
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 269,
                                  "y": 89
                                },
                                {
                                  "x": 280,
                                  "y": 89
                                },
                                {
                                  "x": 280,
                                  "y": 108
                                },
                                {
                                  "x": 269,
                                  "y": 108
                                }
                              ]
                            },
                            "text": "s"
                          }
                        ]
                      }
                    ]
                  }
                ],
                "blockType": "TEXT"
              },
              {
                "boundingBox": {
                  "vertices": [
                    {
                      "x": 12,
                      "y": 122
                    },
                    {
                      "x": 81,
                      "y": 122
                    },
                    {
                      "x": 82,
                      "y": 206
                    },
                    {
                      "x": 13,
                      "y": 206
                    }
                  ]
                },
                "paragraphs": [
                  {
                    "boundingBox": {
                      "vertices": [
                        {
                          "x": 12,
                          "y": 122
                        },
                        {
                          "x": 81,
                          "y": 122
                        },
                        {
                          "x": 82,
                          "y": 206
                        },
                        {
                          "x": 13,
                          "y": 206
                        }
                      ]
                    },
                    "words": [
                      {
                        "boundingBox": {
                          "vertices": [
                            {
                              "x": 12,
                              "y": 122
                            },
                            {
                              "x": 81,
                              "y": 122
                            },
                            {
                              "x": 81,
                              "y": 179
                            },
                            {
                              "x": 12,
                              "y": 179
                            }
                          ]
                        },
                        "symbols": [
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 12,
                                  "y": 122
                                },
                                {
                                  "x": 43,
                                  "y": 122
                                },
                                {
                                  "x": 43,
                                  "y": 175
                                },
                                {
                                  "x": 12,
                                  "y": 175
                                }
                              ]
                            },
                            "text": "1"
                          },
                          {
                            "property": {
                              "detectedBreak": {
                                "type": "EOL_SURE_SPACE"
                              }
                            },
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 50,
                                  "y": 122
                                },
                                {
                                  "x": 81,
                                  "y": 122
                                },
                                {
                                  "x": 81,
                                  "y": 179
                                },
                                {
                                  "x": 50,
                                  "y": 179
                                }
                              ]
                            },
                            "text": "2"
                          }
                        ]
                      },
                      {
                        "boundingBox": {
                          "vertices": [
                            {
                              "x": 19,
                              "y": 177
                            },
                            {
                              "x": 76,
                              "y": 176
                            },
                            {
                              "x": 76,
                              "y": 205
                            },
                            {
                              "x": 19,
                              "y": 206
                            }
                          ]
                        },
                        "symbols": [
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 19,
                                  "y": 186
                                },
                                {
                                  "x": 38,
                                  "y": 186
                                },
                                {
                                  "x": 38,
                                  "y": 205
                                },
                                {
                                  "x": 19,
                                  "y": 205
                                }
                              ]
                            },
                            "text": "m"
                          },
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 42,
                                  "y": 186
                                },
                                {
                                  "x": 55,
                                  "y": 186
                                },
                                {
                                  "x": 55,
                                  "y": 206
                                },
                                {
                                  "x": 42,
                                  "y": 206
                                }
                              ]
                            },
                            "text": "o"
                          },
                          {
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 58,
                                  "y": 177
                                },
                                {
                                  "x": 60,
                                  "y": 177
                                },
                                {
                                  "x": 60,
                                  "y": 205
                                },
                                {
                                  "x": 58,
                                  "y": 205
                                }
                              ]
                            },
                            "text": "i"
                          },
                          {
                            "property": {
                              "detectedBreak": {
                                "type": "EOL_SURE_SPACE"
                              }
                            },
                            "boundingBox": {
                              "vertices": [
                                {
                                  "x": 65,
                                  "y": 186
                                },
                                {
                                  "x": 76,
                                  "y": 186
                                },
                                {
                                  "x": 76,
                                  "y": 205
                                },
                                {
                                  "x": 65,
                                  "y": 205
                                }
                              ]
                            },
                            "text": "s"
                          }
                        ]
                      }
                    ]
                  }
                ],
                "blockType": "TEXT"
              }
            ]
          }
        ],
        "text": "24\nmois\n12\nmois\n"
      }
    }
  ]
}

你能帮帮我吗?

谢谢大家:)

【问题讨论】:

    标签: json command-line jq


    【解决方案1】:

    response 和 textAnnotations 都是数组,据我所知,您必须遍历它们,或者访问第一个响应元素的第一个 textAnnotation,如下所示:response[0].textAnnotations[0].description

    如果可能,我建议循环播放。

    【讨论】:

      【解决方案2】:

      如果你想要 .responses[].textAnnotations 下的所有描述,你可以写:

      .responses[].textAnnotations[].description 
      

      简称:

      .responses[] | .textAnnotations[] |.description 
      

      这实际上是“for”循环中的“for”循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-15
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多