Question

In: Computer Science

Write a javascript code to replace the JSON body with another JSON body. JSON body1 {...

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"

}

Solutions

Expert Solution

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:


Related Solutions

Write a javascript code to check if the JSON body follows the schema. schema is {...
Write a javascript code to check if the JSON body follows the schema. schema is {     "name":"john doe",     "mood":"happy"     "major":"cs",     "date":"2024" } json body is {     "name":"john doe", "height":"170cm"     "major":"cs",     "date":"2024" } the result should be false
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
Build an application using HTML, CSS, javascript, json. It is not necessary for backend to be...
Build an application using HTML, CSS, javascript, json. It is not necessary for backend to be working but the frontend should be good. App should to simple and useful in our day to day life. For eg- grocery shopping list or alarm or weather forecast or reminder.
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
Python Practice Sample:   Write code to replace every occurrence of THE or the with ### and...
Python Practice Sample:   Write code to replace every occurrence of THE or the with ### and every word ending with the letter s to end with a $. Print the resulting text four words per line (and any remaining words from each paragraph on the last one of each paragraph) "The modern business world goes way beyond the balance sheet. Whether your passion is finance or fashion, economics or the environment, you need an education built for business. At Bentley,...
Write Javascript code for the function malwareFrequencies() that shows the malware analyst a prompt. The analyst...
Write Javascript code for the function malwareFrequencies() that shows the malware analyst a prompt. The analyst enters a list of malware names (separated by spaces) for each malware incident they have heard about in the last month.  Note that the same malware can be involved in multiple incidents. Your function should print those malware names and their frequencies to the screen. Sample output is shown below. Zeus 1 Emotet 3 WannaCry 2 Emotet 3 Emotet 3 WannaCry 2
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project...
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project (starting from scratch). Thank you!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT