简体   繁体   中英

jq: how to add an object, key/value in a nested json tree with arrays

I am pretty new to JQ ... so sorry if it seems obvious.. The bare problem first. I have this JSON file:

Link: https://github.com/mariotti/technical_interview_questions/blob/master/QUESTIONS.json

Extract.

cat QUESTIONS.json | jq '.TechQuestions.category[0,1].question[0,1]'
output:
{
ID: Q1,
categoryname: General,
idC: C1,
idCQ: C1Q1,
idQ: Q1,
title: Find the most frequent integer in an array
}
{
ID: Q21,
categoryname: Strings,
idC: C2,
idCQ: C2Q1,
idQ: Q1,
title: Find the first non-repeated character in a String
}
{
ID: Q2,
categoryname: General,
idC: C1,
idCQ: C1Q2,
idQ: Q2,
title: Find pairs in an integer array whose sum is equal to 10 (bonus; do it in linear time)
}
{
ID: Q22,
categoryname: Strings,
idC: C2,
idCQ: C2Q2,
idQ: Q2,
title: Reverse a String iteratively and recursively
}

As you can see, this is "deep" into:

{
"TechQuestions": {
"category": [
  {
    "catname": "General",
    "idC": "C1",
    "question": [
      {
        "ID": "Q1",
        "categoryname": "General",
        "idC": "C1",
        "idCQ": "C1Q1",
        "idQ": "Q1",
        "title": "Find the most frequent integer in an array"
      },

I want to add the key/field:

"codefile" : "a string to be defined"

within the question[] items to get something like:

      {
        "ID": "Q1",
        "categoryname": "General",
        "idC": "C1",
        "idCQ": "C1Q1",
        "idQ": "Q1",
        "title": "Find the most frequent integer in an array",
        "codefile" : "not present"
      },

And I want to do it programmatically as I might need to develop a bit further...

From other sources ( Transforming the name of key deeper in the JSON structure with jq ) I could for example rename a key with this:

cat QUESTIONS.json | jq '.' | jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
 . as $in
 | if type == "object" then
      reduce keys[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;
(.  |= walk(
           if type == "object"
           then with_entries( if .key == "name" then .key |= sub("name";"title") else . end)
           else .
           end))'

I was trying to modify this bit without success. It seems I am unable to simply add a key/value!

I will avoid to overload you with odd references and a further list of attempts. But maybe I give you an example of a try:

(.  |= walk(
       if type == "object"
       then with_entries(
            if .key == "question"
            then . = ( . + {"freshly": "added"})
            else .
            end)
       else .
       end))'

The solution doesn't have to match my attempts. Actually if there is a more straight full way it is very appreciated.

What's wrong with:

.TechQuestions.category[0,1].question[] += {"codefile" : "a string to be defined"}

Using walk/1 , you could consider:

walk( if type == "object" and has("question")
      then .question[] += {"codefile" : "a string to be defined"}
      else .
      end)

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