使用jq实用程序或命令行的替代方法基于公共密钥加入两个json文件 [英] join two json files based on common key with jq utility or alternative way from command line

查看:83
本文介绍了使用jq实用程序或命令行的替代方法基于公共密钥加入两个json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个具有公共密钥的json文件,我需要使用 jq 实用程序或从命令行通过其他方式加入它们,如下所示:

I have 2 json files with common key and I need to JOIN them with jq utility or alternative way from command line As follows:

(例如:jq -join -key"id" jsonFile1 jsonFile2)

(for example: jq -join -key "id" jsonFile1 jsonFile2)

jsonFile1:

jsonFile1:

{"id":"10","data":"abc"}
{"id":"20","data":"xyz"}
{"id":"30","data":"qwe"}
{"id":"40","data":"wsx"}
{"id":"50","data":"zxc"}

jsonFile2:

jsonFile2:

{"id":"60","content":"ert"}
{"id":"40","content":"tgb"}
{"id":"10","content":"yui"}
{"id":"30","content":"ujm"}
{"id":"70","content":"rfv"}

输出:

{"id":"10","data":"abc","content":"yui"}
{"id":"30","data":"qwe","content":"ujm"}
{"id":"40","data":"wsx","content":"tgb"}

推荐答案

此响应说明了使用jq的有效方法.

This response illustrates an efficient approach using jq.

在该示例中,每个对象中的.id的值是一个字符串 因此,在此响应的第一部分中,假定密钥始终为字符串值(在P.S.中,此假设是宽松的).

In the example, the value of .id in each object is a string and therefore in the first part of this response, it is assumed that the key is always string-valued (in the P.S., this assumption is relaxed).

还假定可以将行"组合在一起而不必考虑相互矛盾的值. (我们使用jq的+组合对象.)

It is also assumed that the "rows" can be combined without regard to conflicting values. (We use jq's + to combine objects.)

# hashJoin(a1; a2; field) expects a1 and a2 to be arrays of JSON objects
# and that for each of the objects, the field value is a string.
# A relational join is performed on "field".

def hashJoin(a1; a2; field):
  # hash phase:
  (reduce a1[] as $o ({};  . + { ($o | field): $o } )) as $h1
  | (reduce a2[] as $o ({};  . + { ($o | field): $o } )) as $h2
  # join phase:
  | reduce ($h1|keys[]) as $key
      ([]; if $h2|has($key) then . + [ $h1[$key] + $h2[$key] ] else . end) ;

hashJoin( $file1; $file2; .id)[]

调用:

$ jq -nc --slurpfile file1 file1.json --slurpfile file2 file2.json -f join.jq

输出:

{"id":"10","data":"abc","content":"yui"}  
{"id":"30","data":"qwe","content":"ujm"}
{"id":"40","data":"wsx","content":"tgb"}

P.S.这是hashJoin/3的一种更有效的实现,它放宽了对指定键"的所有假设,只是它指定了一个有效的键.组合键可以指定为数组.

P.S. Here is a still more efficient implementation of hashJoin/3, which relaxes all assumptions about the specified "key" except that it specify a valid key. Composite keys can be specified as arrays.

def hashJoin(a1; a2; key):
  def akey: key | if type == "string" then . else tojson end;
  def wrap: { (akey) : . } ;
  # hash phase:
  (reduce a1[] as $o ({};  . + ($o | wrap ))) as $h1
  | (reduce a2[] as $o 
      ( {};
        ($o|akey) as $v
        | if $h1[$v] then . + { ($v): $o } else . end )) as $h2
  # join phase:
  | reduce ($h2|keys[]) as $key
      ([];  . + [ $h1[$key] + $h2[$key] ] ) ;

这篇关于使用jq实用程序或命令行的替代方法基于公共密钥加入两个json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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