简体   繁体   English

使用 jq 拆分嵌套 JSON 对象的数组

[英]Split an array of nested JSON objects using jq

I'm trying to split an array of nested json objects using jq .我正在尝试使用jq拆分一组嵌套的 json 对象。 Could someone please help to resolve this issue and provide a better solution?有人可以帮助解决这个问题并提供更好的解决方案吗?

Sample input is:样本输入为:

{
  "storeId": "1412",
  "templateCheck": [
    {
      "rom": 37,
      "updateDate": "2021-05-09 07:53:17",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-19-78-5C",
      "sk": "830066",
      "skUpdateDate": "2020-07-21 05:37:07",
      "battery": 30,
      "status": 1
    },
    {
      "rom": 37,
      "updateDate": "2021-05-09 07:54:02",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-1B-FE-6E",
      "sk": "740541",
      "skUpdateDate": "2021-03-22 05:59:00",
      "battery": 30,
      "status": 1
    },
    {
      "rom": 37,
      "updateDate": "2021-05-09 07:52:05",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-1C-22-6E",
      "sk": "846760",
      "skUpdateDate": "2021-05-08 03:34:22",
      "battery": 29,
      "status": 1
    }
  ]
}

Expecting output as:期望 output 为:

{
    "storeId": "1412",
    "templateCheck": [
      "rom": 37,
      "updateDate": "2021-05-09 07:53:17",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-19-78-5C",
      "sk": "830066",
      "skUpdateDate": "2020-07-21 05:37:07",
      "battery": 30,
      "status": 1
    ]
}
{
    "storeId": "1412",
    "templateCheck": [
      "rom": 37,
      "updateDate": "2021-05-09 07:54:02",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-1B-FE-6E",
      "sku": "740541",
      "skUpdateDate": "2021-03-22 05:59:00",
      "battery": 30,
      "status": 1
    ]
}
{
    "storeId": "1412",
    "templateCheck": [
      "rom": 37,
      "updateDate": "2021-05-09 07:52:05",
      "lhb": "2021-05-09 06:32:41",
      "templateCode": "REGULAR",
      "lId": "50-1C-22-6E",
      "sk": "846760",
      "skUpdateDate": "2021-05-08 03:34:22",
      "battery": 29,
      "status": 1
    ]
}

Sure it's possible.当然有可能。 Assuming there are no other top-level keys that you care about,假设没有您关心的其他顶级键,

jq '.storeId as $storeId | .templateCheck[] | { $storeId, templateCheck: [ . ] }'

does just fine to reproduce your example output, or maybe a little more generically (but more confusingly):可以很好地重现您的示例 output,或者更通用一点(但更令人困惑):

jq '(. | del(.templateCheck)) as $o | .templateCheck[] | $o + { templateCheck: [.] }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM