-1

How can I add, or modify if the element already exists, an element for each object in an array using jq?

For example going from:

[
    {
        "firstname": "Sophie",
        "lastname": "Haydock"
    },
    {
        "firstname": "Toussaint",
        "lastname": "Louverture"
    }
]

to

[
    {
        "firstname": "Sophie",
        "lastname": "Haydock",
        "common": "something"
    },
    {
        "firstname": "Toussaint",
        "lastname": "Louverture",
        "common": "something"
    }
]

This is different from the question marked as duplicate in that it adds an element to an object in an array rather than add an object to an array.

Nils André
  • 571
  • 5
  • 17

1 Answers1

3

To do something with each element in an array, use map:

jq 'map(.common="something")'
that other guy
  • 116,971
  • 11
  • 170
  • 194