简体   繁体   中英

Parse 2 files based on key value and recreate another json file [JQ]

I am new to JQ. I need to make a json file based on another 2 files. I am worked with it whole day and stack here. Badly need this.

Here is file 1

{
"name": "foo",
  "key": "1",
  "id": "x"
}
{
  "name": "bar",
  "key": "2",
  "id": "x"
}
{
  "name": "baz",
  "key": "3",
  "id": "y"
}

file 2

{
"name": "a",
  "key": "1"
}
{
  "name": "b",
  "key": "1"
}
{
  "name": "c",
  "key": "2"
}
{
  "name": "d",
  "key": "2"
}
{
  "name": "e",
  "key": "3"
}

Expected Result:

{
"x": {
    "foo": [
      "a",
      "b"
    ],
    "bar": [
      "c",
      "d"
    ]
  },
  "y": {
    "baz": [
      "e"
    ]
  }
}

I can do it with python script but I need it with jq.

Thanks in advance.

Use reduce on the first file's items ( $i ) to successively build up the result object using setpath with fields from the item and values as a matching map on the secondary dictionary file ( $d ).

jq -s --slurpfile d file2 '
  reduce .[] as $i ({}; setpath(
    [$i.id, $i.name];
    [$d[] | select(.key == $i.key).name]
  ))
' file1

For efficiency, the following solution first constructs a "dictionary" based on file2; furthermore, it does so without having to "slurp" it.

< file2 jq -nc --slurpfile file1 file1 '
  (reduce inputs as {$name, $key} ({};
      .[$key] += [$name])) as $dict
  | reduce $file1[] as {$name, $key, $id} ({};
      .[$id] += [ {($name): $dict[$key]} ] )
'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM