29

json file as follows:

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

now i want to add another json object {"location":"canada"} at the end of the file using bash script i have tried echo "{"location":"canada"}">>sample.json

but it results

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

but i want it to be like this

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

please suggest me

1
  • Shell script is not very well suited for this task. You should try to find a tool which really understands JSON. If you know Python, have a look at json.tool. Commented May 30, 2013 at 5:14

2 Answers 2

70

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"
}
Sign up to request clarification or add additional context in comments.

4 Comments

I've only worked with jq for 5 minutes but it's hella flexible. I would highly recommend this over doing complicated stuff with ed/sed/awk.
jq '.location="canada"' sample.json was the only thing that worked, but it didn't append to sample.json; it just prints it to the screen. The whole idea was we wanted to append to a son file. jq -s add keeps giving "parse error: Invalid numeric literal at line 3, column 32". Even if you delete "age":23, you still get "invalid numeric literal". How did this work for you guys?
@Bear: all examples in the answer work (I've just checked again on the current jq version on my machine: jq --version -> jq-1.5-1-a5b5cbe). All examples print to the screen (you don't want to corrupt your input data while you're adapting the filter for your case. If you don't know how to save the output to a file; ask). I see that someone has upvoted your comment. Does it mean there is an environment where jq doesn't work?
For your convenience jq ... INPUT > INPUT.tmp && mv INPUT.tmp INPUT stackoverflow.com/questions/36565295/…
18
sed -i '$s/}/,\n"location":"canada"}/' sample.json

Result:

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

8 Comments

thanks it's working perfectly i have one more requirement i want to append one json object value to another object. for example i want to append "doctor" to canada it should result {"name" :"sam", "age":23, "designation":"doctor", "location":"doctorcanada"}
@kampu: using sed to manipulate json data with regexes is brittle and unnecessary complicated. There are better alternatives.
Just FYI -- If you do sed -i... on a Mac, you get this error: "sed: 1: "sample.json": unterminated substitute pattern". But if you do it in Linux (Ubuntu), you're fine.
Anyone know how to use a variable in that statement? If you do: loc="canada", and then do sed -i '$s/}/,\n"location":"$loc"}/' sample.json, it doesn't work. Instead of rendering the variable it puts "location":"$loc".
'$s/}/,\n"location":"'$loc'"}/'
|

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.