Home > Software design >  Parse 2 files based on key value and recreate another json file [JQ]
Parse 2 files based on key value and recreate another json file [JQ]

Time:10-31

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.

CodePudding user response:

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

CodePudding user response:

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]} ] )
'
  • Related