[英]Multilple csv files joining to json output?
Thanks in advance for your support. 预先感谢您的支持。
I did some googling but nothing helped me. 我做了一些谷歌搜索,但没有任何帮助。
I have two input csv files having one to many relationship (parent child). 我有两个输入CSV文件,它们之间存在一对多的关系(父级子级)。
How to create json ouput for each record in the master with child fields from those two files ? 如何使用来自这两个文件的子字段为主文件中的每个记录创建json输出? while googling, I can see samples that say 1 single csv file to json but not multiple.
在谷歌搜索时,我可以看到一些示例,其中将1个单个csv文件转换为json,但没有多个。
Here is an example 这是一个例子
Parent File Employee
emp_no emp_name
1001 jhon
1002 mike
Child file Reportees
emp_no master_emp_no emp_name
1010 1001 x
1011 1001 y
1012 1001 z
1013 1002 A
1014 1002 B
I need a json object for each entry in Parent table that includes child table details of that parent 对于父表中的每个条目,我都需要一个json对象,其中包括该父表的子表详细信息
first json object ----> 1001,john,[1010 x,1011 y,1012 z]
second json object ----> 1002,john,[1013 A,1014 B]
Please advice. 请指教。
Here is a solution using jq 这是使用jq的解决方案
If the file filter.jq
contains 如果文件
filter.jq
包含
def parse:
reduce ( inputs # reads input tsv files and returns object
| { # {
f:input_filename, # "parent": [["emp_no",...], ["1001", "jho...
r:split("\t") # "child": [["emp_no","master_emp_no",...
} # }
| select(.r|length>0)) as $i
{}
; .[$i.f] += [$i.r]
)
;
def restructure:
[ # converts [["emp_no", "emp_name"],
.[0] as $h # ["1001", "jhon"], ["1002", "mike"]]
| .[1:][] as $v # to [{"emp_no":"1001","emp_name":"jhon"},
| [ [$h, $v] # {"emp_no":"1002","emp_name":"mike"}]
| transpose[]
| {key:.[0], value:.[1]}
] | from_entries
]
;
def format:
.child as $c # constructs the final output
| .parent # emp_no,emp_name,children
| map(.children = ( # 1001,jhon,["1010 x","1011 y","1012 z"]
.emp_no as $e # 1002,mike,["1013 A","1014 B"]
| $c
| map(select($e == .master_emp_no)
| "\(.emp_no) \(.emp_name)")
| tojson
)
)
| (.[0] | keys_unsorted), .[]
| join(",")
;
parse
| map_values(restructure)
| format
the file parent
contains tab-separated values parent
文件包含制表符分隔的值
emp_no emp_name
1001 jhon
1002 mike
and the file child
contains tab-separated values 并且文件
child
包含制表符分隔的值
emp_no master_emp_no emp_name
1010 1001 x
1011 1001 y
1012 1001 z
1013 1002 A
1014 1002 B
then the command 然后命令
jq -M -R -n -r -f filter.jq parent child
will produce 将产生
emp_no,emp_name,children
1001,jhon,["1010 x","1011 y","1012 z"]
1002,mike,["1013 A","1014 B"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.