Skip to main content
Became Hot Network Question
Add syntax hints, add relevant tag
Source Link
AdminBee
  • 23.3k
  • 25
  • 52
  • 76

I have a huge JSON object with an array of objects inside it. I have to add key:value pair to a specific object in the array. For example, let the input object is:

{
  "a": {
    "b": [
      {
        "name": "name1",
        "value": 1,
        "param": {
          "p1": "par1"
        }
      },
      {
        "name": "name2",
        "value": 2,
        "param": {
          "p1": "par2"
        }
      },
      {
        "name": "name3",
        "value": 3,
        "param": {
          "p1": "par3"
        }
      }
    ],
    "c": 4,
    "d": 5
  }
}
{
  "a": {
    "b": [
      {
        "name": "name1",
        "value": 1,
        "param": {
          "p1": "par1"
        }
      },
      {
        "name": "name2",
        "value": 2,
        "param": {
          "p1": "par2"
        }
      },
      {
        "name": "name3",
        "value": 3,
        "param": {
          "p1": "par3"
        }
      }
    ],
    "c": 4,
    "d": 5
  }
}

Using index the modification is easy:

 jq '.a.b[0].param += {new: "QQQ"}'
 jq '.a.b[0].param += {new: "QQQ"}'

But I can not be 100% sure of index. I have to specify the object with the name tag .name == "name1".

How to modify an object identified by name tag?

I have a huge JSON object with an array of objects inside it. I have to add key:value pair to specific object in array. For example, let the input object is:

{
  "a": {
    "b": [
      {
        "name": "name1",
        "value": 1,
        "param": {
          "p1": "par1"
        }
      },
      {
        "name": "name2",
        "value": 2,
        "param": {
          "p1": "par2"
        }
      },
      {
        "name": "name3",
        "value": 3,
        "param": {
          "p1": "par3"
        }
      }
    ],
    "c": 4,
    "d": 5
  }
}

Using index the modification is easy:

 jq '.a.b[0].param += {new: "QQQ"}'

But I can not be 100% sure of index. I have to specify the object with the name tag .name == "name1".

How to modify an object identified by name tag?

I have a huge JSON object with an array of objects inside it. I have to add key:value pair to a specific object in the array. For example, let the input object is:

{
  "a": {
    "b": [
      {
        "name": "name1",
        "value": 1,
        "param": {
          "p1": "par1"
        }
      },
      {
        "name": "name2",
        "value": 2,
        "param": {
          "p1": "par2"
        }
      },
      {
        "name": "name3",
        "value": 3,
        "param": {
          "p1": "par3"
        }
      }
    ],
    "c": 4,
    "d": 5
  }
}

Using index the modification is easy:

 jq '.a.b[0].param += {new: "QQQ"}'

But I can not be 100% sure of index. I have to specify the object with the name tag .name == "name1".

How to modify an object identified by name tag?

Source Link

Update object inside array inside another JSON object

I have a huge JSON object with an array of objects inside it. I have to add key:value pair to specific object in array. For example, let the input object is:

{
  "a": {
    "b": [
      {
        "name": "name1",
        "value": 1,
        "param": {
          "p1": "par1"
        }
      },
      {
        "name": "name2",
        "value": 2,
        "param": {
          "p1": "par2"
        }
      },
      {
        "name": "name3",
        "value": 3,
        "param": {
          "p1": "par3"
        }
      }
    ],
    "c": 4,
    "d": 5
  }
}

Using index the modification is easy:

 jq '.a.b[0].param += {new: "QQQ"}'

But I can not be 100% sure of index. I have to specify the object with the name tag .name == "name1".

How to modify an object identified by name tag?