how to add json object to json file using shell script

后端 未结 2 1442
情深已故
情深已故 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"
    }
    
    0 讨论(0)
  • 2021-01-01 15:34
    sed -i '$s/}/,\n"location":"canada"}/' sample.json
    

    Result:

    {"name" :"sam",
    "age":23,
    "designation":"doctor",
    "location":"canada"}
    
    0 讨论(0)
提交回复
热议问题