代码之家  ›  专栏  ›  技术社区  ›  Elliot Jeremy Allen

用jq解析一组json对象

  •  1
  • Elliot Jeremy Allen  · 技术社区  · 7 年前

    A有一个很大的JSON块,我正在尝试解析,它基本上看起来像

    {
      "order": [
        "hash1",
        "hash2"
      ],
      "posts": {
        "hash4": {
          "id": "hash4",
          "message": "lorem ipsem"
        },
        "hash5": {
          "id": "hash5",
          "message": "dolor sit amet"
        },
        "hash6": {
          "id": "hash6",
          "message": "consectetur adipiscing elit"
        }
      }
    }
    

    到目前为止,我处理这件事的方式是只处理好消息。

    $ grep 'message' jq_dat.json 
          "message": "lorem ipsem"
          "message": "dolor sit amet"
          "message": "consectetur adipiscing elit"
    

    这对我目前的目的有效,但我想知道如何取得同样的效果 jq . 即。

    $ jq .posts.<something>.message < jq_dat.json
    "lorem ipsem"
    "dolor sit amet"
    "consectetur adipiscing elit"
    

    我试过用 [] 和 {} 代替 something 但这两个都会返回编译错误。

    1 回复  |  直到 7 年前
        1
  •  2
  •   taygetos blackbot    7 年前

    你只是有一个太多的点

       jq .posts[].message < jq_dat.json