根据密钥获取数据

时间:2017-07-14 16:48:26

标签: json perl unix key

{ "Labels": [ {"Test": 99.25341796875, "Name": "Skateboard" }, { "Test": 9.25341796875, "Name": "Sport" }, { "Test": 49.24723052978516, "Name": "People" }]}

我需要根据以下条件删除Testtag 如果测试值> 50,则替换Test = Major 如果测试值<50,则替换为Test = Minor

所以这里请求的输出如下所示。

{ "Labels": [ {"High": 99.25341796875, "Name": "Skateboard" }, { "Low": 9.25341796875, "Name": "Sport" }, { "Low": 49.24723052978516, "Name": "People" }]}

1 个答案:

答案 0 :(得分:1)

jq解决方案:

jq '.Labels |= map(.[(if .Confidence > 50 then "High" else "Low" end)]= .Confidence | del(.Confidence))' yourfile.json

输出:

{
  "Labels": [
    {
      "Name": "Skateboard",
      "High": 99.25341796875
    },
    {
      "Name": "Sport",
      "Low": 9.25341796875
    },
    {
      "Name": "People",
      "Low": 49.24723052978516
    }
  ]
}