Multilple csv files joining to json output? -


thanks in advance support.

i did googling nothing helped me.

i have 2 input csv files having 1 many relationship (parent child).

how create json ouput each record in master child fields 2 files ? while googling, can see samples 1 single csv file json not multiple.

here 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           1014   1002           b 

i need json object each entry in parent table includes child table details of parent

first json object ---->  1001,john,[1010 x,1011 y,1012 z] second json object ---->  1002,john,[1013 a,1014 b] 

please advice.

here solution using jq

if file filter.jq contains

def parse:   reduce ( inputs                     # reads input tsv files , returns object          | {                          # {             f:input_filename,         #   "parent": [["emp_no",...], ["1001", "jho...             r:split("\t")             #   "child": [["emp_no","master_emp_no",...            }                          # }          | select(.r|length>0)) $i     {}   ; .[$i.f] += [$i.r]   ) ;  def restructure:   [                                   # converts [["emp_no", "emp_name"],     .[0]    $h                     #           ["1001", "jhon"], ["1002", "mike"]]   | .[1:][] $v                     #       [{"emp_no":"1001","emp_name":"jhon"},   | [   [$h, $v]                      #           {"emp_no":"1002","emp_name":"mike"}]       | transpose[]       | {key:.[0], value:.[1]}     ] | from_entries  ] ;  def format:     .child $c                      # constructs final output   | .parent                           # emp_no,emp_name,children   | map(.children = (                 # 1001,jhon,["1010 x","1011 y","1012 z"]             .emp_no $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

emp_no  emp_name 1001    jhon 1002    mike 

and file child contains tab-separated values

emp_no  master_emp_no   emp_name 1010    1001    x 1011    1001    y 1012    1001    z 1013    1002    1014    1002    b 

then 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"] 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -