how to add json object to json file using shell script

后端 未结 2 1441
情深已故
情深已故 2021-01-01 14:57

json file as follows:

{\"name\" :\"sam\",
\"age\":23,
\"designation\":\"doctor\"}

now i want to add another json object {\"location\":\"can

2条回答
  •  生来不讨喜
    2021-01-01 15:33

    To merge two json objects, you could use jq command-line utility:

    $ jq -s add sample.json another.json
    

    Output:

    {
      "name": "sam",
      "age": 23,
      "designation": "doctor",
      "location": "canada"
    }
    

    To update a single attribute:

    $ jq '.location="canada"' sample.json
    

    It produces the same output.

    To prepend "doctor" to the location:

    $ jq '.location = "doctor" + .location' input.json
    

    Output:

    {
      "name": "sam",
      "age": 23,
      "designation": "doctor",
      "location": "doctorcanada"
    }
    

提交回复
热议问题