Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

jq: Conditional insert using "lookup" & "target" JSON objects

I'm trying to improve a bash script I wrote using jq (Python version), but can't quite get the conditional nature of the task at hand to work.

The task: insert array from one JSON object ("lookup") into another ("target") only if the key of the "lookup" matches a particular "higher-level" value in the "target". Assume that the two JSON objects are in lookup.json and target.json, respectively.

A minimal example to make this clearer:

"Lookup" JSON:

{
   "table_one": [
      "a_col_1",
      "a_col_2"
   ],
   "table_two": [
      "b_col_1",
      "b_col_2",
      "b_col_3"
   ]
}

"Target" JSON:

{
  "top_level": [
    {
      "name": "table_one",
      "tests": [
        {
          "test_1": {
            "param_1": "some_param"
          }
        },
        {
          "test_2": {
            "param_1": "another_param"
          }
        }]
    },
    {
      "name": "table_two",
      "tests": [
        {
          "test_1": {
            "param_1": "some_param"
          }
        },
        {
          "test_2": {
            "param_1": "another_param"
          }
        }
      ]
    }
  ]
}

I want the output to be:

{
    "top_level": [{
                "name": "table_one",
                "tests": [{
                        "test_1": {
                            "param_1": "some_param"
                        }
                    },
                    {
                        "test_2": {
                            "param_1": "another_param",
                            "param_2": [
                                "a_col_1",
                                "a_col_2"
                            ]
                        }
                    },
                    {
                        "name": "table_two",
                        "tests": [{
                                "test_1": {
                                    "param_1": "some_param"
                                }
                            },
                            {
                                "test_2": {
                                    "param_1": "another_param",
                                    "param_2": [
                                        "b_col_1",
                                        "b_col_2",
                                        "b_col_3"
                                    ]
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }

Hopefully, that makes sense. Early attempts slurped both JSON blobs and assigned them to two variables. I'm trying to select for a match on [roughly] ($lookup | keys[]) == $target.top_level.name, but I can't quite get this match or the subsequent the array insert working.

Any advice is well-received!

question from:https://stackoverflow.com/questions/65912048/jq-conditional-insert-using-lookup-target-json-objects

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming the JSON samples have been corrected, and that the following program is in the file "target.jq", the invocation:

jq --argfile lookup lookup.json -f target.jq target.json

produces the expected result.

target.jq

.top_level |= map(
  $lookup[.name] as $value
  | .tests |= map(
      if has("test_2")
      then .test_2.param_2 = $value
      else . end) )

Caveat

Since --argfile is officially deprecated, you might wish to choose an alternative method of passing in the contents of lookup.json, but --argfile is supported by all extant versions of jq as of this writing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...