从具有共同父项

时间:2017-06-12 08:47:55

标签: arrays json jq

可能是JQ: Nested JSON transformation的一个接近的问题,但我无法理解它。

示例JSON:

"value": [
{
  "FeatureStatus": [
    {
      "FeatureName": "Sway1",
      "FeatureServiceStatus": "ServiceOperational"
    },
    {
      "FeatureName": "Sway2",
      "FeatureServiceStatus": "ServiceDegraded"
    }
  ],
  "Id": "SwayEnterprise",
},
{
  "FeatureStatus": [
    {
      "FeatureName": "yammerfeatures",
      "FeatureServiceStatus": "ServiceOperational"
    }
  ],
  "Id": "yammer"
}
]

我想要做的是使用jq创建一个输出,结果如下:

{"Sway":"Sway1":"ServiceOperational"},
{"Sway":"Sway2":"ServiceDegraded"},
{"Yammer":"yammerfeatures":"ServiceOperational"}

我的各种尝试最终导致成千上万的非唯一(即Yammer with Sway状态),或者只有一个Id具有x号的FeatureServiceStatus。

任何指针都将非常感激。我已经阅读了教程和食谱。我可能需要2.5天才能使用jq。

1 个答案:

答案 0 :(得分:0)

Assuming that the enclosing braces have been added to make the input valid JSON, the filter:

.value[]
| [.Id] + (.FeatureStatus[] | [ .FeatureName, .FeatureServiceStatus ])

produces:

["SwayEnterprise","Sway1","ServiceOperational"]
["SwayEnterprise","Sway2","ServiceDegraded"]
["yammer","yammerfeatures","ServiceOperational"]

You can then easily reformat this as desired.