1

I have a json object of the form:

{
  ...
  objectx: {
    nested_obj1: {
      key1: value1
    },
    nested_obj2: {
      key2: value2
    },
    ...
  },
  ...
}

. I need to add a keyX: valueX to all the nested_objs in objectx using jq.

I was trying to apply a map filter using .objectx | map(.+{keyX: valueX}) but could not figure out how to store this filtered list into the original json object. How can I achieve this?

1 Answer 1

2

Select the objects you wish to update (the values of the objectx object) and set the value you want. map is designed to work on arrays, not objects. map_values can be used for that instead.

.objectx |= map_values(.keyX = $valueX)

Personally I prefer do this:

.objectx[].keyX = $valueX

Note that using [] on an object will yield all values of that object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.