In: Computer Science
Write a javascript code to replace the JSON body with another JSON body.
JSON body1
{
"name":"john doe",
"mood":"happy"
"major":"cs",
"date":"2024"
}
json body2 is
{
"mood":"sad"
"major":"accounting",
"date":"2023"
} the result should be
{
"name":"john doe",
"mood":"sad"
"major":"accounting",
"date":"2023"
}
json1 =
{
"name":"john doe",
"mood":"happy",
"major":"cs",
"date":"2024"
}
json2 =
{
"mood":"sad",
"major":"accounting",
"date":"2023"
}
json1["mood"] = json2["mood"]
json1["major"] = json2["major"]
json1["date"] = json2["date"]
To access the values of a json object in javascript, we use the name of the key inside the square brackets. Using this rule, we replace the values of mood, major and date in json1 with that from json2.
To verify if the resulting json is correct, we can use the console.log(json1).
Code & Output: