首页 > 解决方案 > 用 jq 解析一组 JSON 对象

问题描述

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 消息

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

这适用于我目前的目的,但我想知道如何使用jq. IE

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

我尝试使用[]and{}代替something,但是它们都吐回编译错误。

标签: jsongrepjq

解决方案


你只是有一个太多的点

jq .posts[].message < jq_dat.json

推荐阅读