使用jq从JSON删除重复值 [英] Remove duplicate values from JSON with jq

查看:134
本文介绍了使用jq从JSON删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm merging two json-files with jq

(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": ""
      }
    ]
  }
]

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

but now i want to get rid of the duplicate entries which have the same string in their search-attribute.

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

I've tried unique and unique_by filters, but it leads to complie errors.

结果,应为:

  [
  {
    "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有可能吗?因为我不是在尝试过滤键,而是正常值.有什么想法吗?

is this possible with jq? because i'm not trying to filter keys but normal values. any ideas?

JQPlay

推荐答案

您可以使用以下方法简单地扩展过滤器:

You could simply extend your filter with this:

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

这篇关于使用jq从JSON删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆