Contrast two JSON data sets

Similar Question:
Object comparison in JavaScript

Is there a way to compare two JSON objects and identify any differences in the data they contain?

Update

After going through the feedback, some additional details are required.

  1. A JSON object is described as

    "an unordered set of name/value pairs. An object starts with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma)." -- json.org

  2. What I aim to do is simply compare two JSON object literals.

Not being an expert in JavaScript, if these are referred to as object literals in JavaScript, then that's the term I should use.

I think what I need is a method that can:

  1. Perform deep recursion to detect unique name/value pairs
  2. Determine the length of both object literals, and check for discrepancies in the name/value pairs between them.

Answer №1

Merely parsing the JSON and comparing the two objects may not suffice as they may not be the same object references (though their values could be identical).

A deep equals comparison is necessary.

Referencing here, which appears to utilize jQuery.

Object.extend(Object, {
   deepEquals: function(o1, o2) {
     var k1 = Object.keys(o1).sort();
     var k2 = Object.keys(o2).sort();
     if (k1.length != k2.length) return false;
     return k1.zip(k2, function(keyPair) {
       if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){
         return deepEquals(o1[keyPair[0]], o2[keyPair[1]])
       } else {
         return o1[keyPair[0]] == o2[keyPair[1];
       }
     }).all();
   }
});

Usage:

var anObj = JSON.parse(jsonString1);
var anotherObj= JSON.parse(jsonString2);

if (Object.deepEquals(anObj, anotherObj))
   ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Teach Google Bot how to recognize AJAX content

I'm facing a major issue while developing a website for someone else. The problem lies in the modals that are supposed to open when a user clicks on a product. My goal is to ensure that Google Bot recognizes these modals as individual pages. When a mo ...

The CSS formatting is not being properly applied within the innerHTML

I have a scenario where I am trying to display a Bootstrap card using innerHTML in my TS file, but the styles are not being applied to this content. I suspect that the issue might be because the styles are loaded before the component displays the card, cau ...

The React-FontAwesome icon is unable to display when a favicon has been set

I encountered an issue while using the react-fontawesome module to display brand icons. Whenever I set a favicon in <Head>...</Head> (imported from next/head), all the react-fontawesome icons disappear. Can someone please advise me on how to re ...

The supertest request body cannot be found

Testing my express server POST endpoint using supertest has been a challenge for me. Although everything works perfectly in postman, I encountered an issue when trying to pass body parameters into the test. It seems like the body parameters are not being p ...

What is the process for deleting a response from a ConfirmIT survey?

Our surveys frequently utilize a scale/list called list1, which contains numerous options. For a specific question (Answer code 125), we need to remove one of the options from the list. However, we still want to keep it in the list for other questions. I ...

Creating a dynamic configuration for service instantiation in Angular 4/5

Currently working on a library that has an AuthModule with an AuthService for managing OAuth2 authentication using oidc-client-js. I want the application using this library to be able to set up the configuration for the OAuth client. One way to do this is ...

Will a function take up space for each individual instance of a single class in JavaScript?

Let's think about this class: class A { num: number; str: string; someFunction(){console.log("SomeFunction called!")} } The memory allocation for the someFunction method is a question mark. Is this function stored in memory only once for all ...

A step-by-step guide on displaying log files using NanoHTTPD

I have developed a Java desktop application that can receive HTTP requests using the embedded NanoHTTPD web server from https://github.com/NanoHttpd/nanohttpd. Once an HTTP request is received, my application performs certain tasks and logs the activity to ...

How to determine if an Angular list has finished rendering

I am facing an issue where I have a large array that is being loaded into a ul list using ng-repeat in Angular. The loading of the list takes too long and I want to display a loader while it's loading, but hide it only when the ul list is fully render ...

Utilize the results of the "event's" output as a variable

Struggling with manipulating checkbox values? The `change` event on checkboxes might return an object like this: {"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"} To transform the above object into a format that can be co ...

Step-by-step guide on sending JSON data to a server using AJAX and jQuery

Encountering Error While Trying to Send 'Order' JSON Data to JSON File on Node.js Server Everything seems fine with the GET request, but when attempting a POST request, the 'Error' function is triggered instead of the 'success&apo ...

JavaScript: Collection of Pictures

I've recently begun my journey into HTML and JavaScript by working on a basic website that showcases four images using a for loop. However, upon viewing the website in a browser, I noticed that only the names of the images are displayed, not the actua ...

The issue of nested tabs in Bootstrap 3 causing problems with sliders within the main tab has

Hello, I am utilizing Bootstrap 3 tabs. Within the nav-tabs, there are three tab-panes: the first tab contains text only, the second tab includes a slider, and the third tab has nested tabs. However, I encountered an issue where the slider in the nested t ...

What is causing the TypeError in Discord.js when trying to read the 'voice' property? Any help troubleshooting this issue would be greatly appreciated

Hey everyone, I'm having an issue with my play.js file that I obtained from a Source Bin. If anyone could provide some assistance, it would be greatly appreciated. const ytdl = require("ytdl-core"); const ytSearch = require("yt-search"); //Global que ...

Accessing JSON file content from Firebase storage

I'm currently working on extracting the data from a JSON file stored in my Firebase in order to utilize it for searching within my Google action. So far, I have managed to retrieve the file, but I'm unsure of which method to employ next. I am loo ...

Issues with CSS and JS rendering have been observed on a website hosted through BlueHost

Just set up my website with Bluehost and running into issues with CSS and JS not rendering properly. Here's a glimpse of my public_html folder structure: https://i.sstatic.net/VnHFP.png The assets folder includes separate folders for CSS and JS fil ...

Leveraging an array to store data within highcharts

When I input the data directly in high charts, it works perfectly: $(function () { $('#container').highcharts({ title: { text: 'Monthly Average Temperature', x: -20 //center }, subtit ...

Images displayed alongside webpage contents

I'm having trouble getting these photos to display in the same row. Any suggestions on what I should change? https://i.stack.imgur.com/EbvmR.png I've attempted using float left and other commands, but one picture continues to stay in the lower r ...

Asking for a website link to utilize when sending a request in Node.js

I'm trying to create a user prompt for entering a URL and then making an HTTP request with the entered URL. However, I keep encountering an error that reads: "TypeError: Cannot read property 'parent' of undefined." Interestingly, when I har ...

Issue with event listener not functioning properly with dynamically created content using AJAX (only using vanilla JavaScript

I used pure javascript AJAX to dynamically load content into the "test" div. However, when I try to click on a child div at index 6, an alert box is not being displayed as expected. How can I fix the issue with the click event not working? The gets functi ...