Question

In: Computer Science

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

Solutions

Expert Solution

Hey there ,

I've added code below to check json body for the example given by you. please find the code below . if you find any difficulty or face any issue please comment down or try to connect with me.

Approach 1:

  
var SchemaToBeChecked = {
    "name": "john doe",
    "mood": "happy",
    "major": "cs",
    "date": "2024"
  };
  var JsonBody = {
    "name": "john doe",
    "height":"170cm",
    "major": "cs",
    "date": "2024"
  };

  var flag  = JSON.stringify(SchemaToBeChecked) === JSON.stringify(JsonBody); 
  console.log(flag)

Output:

Approach 2:

1. In this approach you need to use lodesh Module which provide _isEqual (json1 , json2) method which will check the schema.

Example Code for that is given below.

_.isEqual(one, two); // true or false

Approach 3:

Check only keys:

var JsonBody = {
   "name": "john doe", 
"mood":"sad",
 "major": "cs",
  "date": "2024" }; 
var SchemaToBeChecked = { 
  "name": "john doe",
 "mood": "happy",
  "major": "cs", 
  "date": "2024" };
keyObj1 = Object.keys(SchemaToBeChecked);
keyObj2 = Object.keys(JsonBody);
let flag = false;
// now compare their keys 
if (keyObj1.length == keyObj2.length) {
  for (var i = 0; i < keyObj1.length; i++) {
    if (keyObj1[i] == keyObj2[i]) {
      flag = true;
    } else {
      flag = false;
      break;
    }
  }
}

console.log(flag)

Output :


Related Solutions

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" }
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...
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
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.
The schema for the Academics database is as follows. Understanding this schema is necessary to answer...
The schema for the Academics database is as follows. Understanding this schema is necessary to answer the questions in Part B. DEPARTMENT(deptnum, descrip, instname, deptname, state, postcode) ACADEMIC(acnum, deptnum*, famname, givename, initials, title) PAPER(panum, title) AUTHOR(panum*, acnum*) FIELD(fieldnum, id, title) INTEREST(fieldnum*, acnum*, descrip) The semantics of most attributes are self-explanatory. For each relation, the primary key is underlined and any foreign keys are denoted by an asterisk (*). Some additional information for relations is given below: DEPARTMENT: Each academic department...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT