使用 jq 重塑 json 数据

reshape json data using jq

我正在尝试重塑 json 文档,我认为使用 jq 会很容易,但我已经尝试了几个小时,但没有成功...

(请注意我不是 jq jedi 并且文档没有帮助)

我想从这里开始:

{
  "results": [
    {
      "profile": {
        "birthYear": 1900,
        "locale": "en_EN",
        "city": "Somewhere, Around",
        "timezone": "2",
        "age": 52,
        "gender": "m"
      },
      "UID": "SQSQSQerl7XSQSqSsqSQ"
    }
  ]
}

为此:

{
   "birthYear": 1900,
   "locale": "en_EN",
   "city": "Somewhere, Around",
   "timezone": "2",
   "age": 52,
   "gender": "m",
   "UID": "SQSQSQerl7XSQSqSsqSQ"
}

我使用这个过滤器得到了以下内容:.results[].profile , .results[].UID

{
   "birthYear": 1900,
   "locale": "en_EN",
   "city": "Somewhere, Around",
   "timezone": "2",
   "age": 52,
   "gender": "m"   
}
"UID": "SQSQSQerl7XSQSqSsqSQ"

在此先感谢您的帮助..

可能有更好的方法,但给你

jq '.results[0].profile * .results[0] | del(.profile)'


说明:

通过 A * B 将容器与嵌套容器递归合并,然后通过管道传输到 del( 以删除嵌套容器

您可以使用加法运算符组合两个对象。

jq '.results[] | .profile + {UID}'

.profile 已经是一个对象。

另一个对象是使用 {} 创建的。 {UID} 是 shorthand 对于 {"UID" : .UID}