使用jq从JSON删除重复值

时间:2018-11-21 10:23:55

标签: unix jq

我正在用jq合并两个json文件

(group_by( [."contraction", "definitions"]) | map((.[0]|del(."definitions" [])) + { "definitions": (map(."definitions" [])) }))

这将导致以下结果:

  [
  {
    "contraction": "TEST_1",
    "definitions": [
      {
        "search": "1",
        "replace": "12"
      },
      {
        "search": "3",
        "replace": "4"
      },
      {
        "search": "6",
        "replace": "2"
      },
      {
        "search": "1",
        "replace": "1"
      }
    ]
  },
  {
    "contraction": "TEST_2",
    "definitions": [
      {
        "search": "A",
        "replace": "post"
      },
      {
        "search": "B",
        "replace": "prae"
      },
      {
        "search": "A",
        "replace": ""
      }
    ]
  }
]

但是现在我想摆脱在搜索属性中具有相同字符串的重复条目。

我尝试了unique和unique_by过滤器,但这会导致compile错误。

结果,应为:

  [
  {
    "contraction": "TEST_1",
    "definitions": [
      {
        "search": "1",
        "replace": "12"
      },
      {
        "search": "3",
        "replace": "4"
      },
      {
        "search": "6",
        "replace": "2"
      }
    ]
  },
  {
    "contraction": "TEST_2",
    "definitions": [
      {
        "search": "A",
        "replace": "post"
      },
      {
        "search": "B",
        "replace": "prae"
      }
    ]
  }
]

jq有可能吗?因为我不是要过滤键而是正常值。有什么想法吗?

JQPlay

上的代码

1 个答案:

答案 0 :(得分:1)

您可以简单地通过以下方式扩展过滤器:

map( .definitions |= unique_by(.search) )