Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JSON Articles
Page 3 of 16
Compare keys & values in a JSON object when one object has extra keys in JavaScript
When comparing JSON objects in JavaScript, you often need to check if the common keys have matching values, even when one object has extra properties. This is useful for validating partial object matches or checking if one object is a subset of another. Problem Statement Consider these two objects: const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"}; We need a function that returns true because all common keys (a, b, c) have matching values, ignoring the extra keys (e, d) ...
Read MoreSearch by id and remove object from JSON array in JavaScript
Suppose, we have an array of objects that contains data about some movies like this − const arr = [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "action"}, {id: "4", name: "Ordinary People", type: "drama"}, {id: "5", name: "Billy Elliot", type: "drama"}, {id: "6", name: "Toy Story", type: "children"} ]; We are required to write ...
Read MoreGroup Similar Items in JSON in JavaScript
Suppose, we have a JSON Array that contains data about some tickets like this: const arr = [ { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": ...
Read MoreCalculate average from JSON data based on multiple filters JavaScript
When working with JSON data, you often need to filter, group, and calculate averages based on multiple criteria. This article demonstrates how to group objects by multiple fields and compute averages while handling edge cases like undefined values. Problem Statement Given an array of supplier objects, we need to: Group objects with the same "SupplierName" and "Category" Sum their points together (ignoring undefined values) Calculate the average points for each group Return the grouped results with totals and averages ...
Read MoreJavaScript: create an array of JSON objects from linking two arrays
Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...
Read MoreConvert JSON array into normal json in JavaScript
Sometimes you receive data in a complex JSON array format with key-value pairs, but need to convert it to a simple flat object structure for easier access and manipulation. Problem Structure Consider this complex JSON array with nested key-value objects: const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": ...
Read MoreBuild tree array from JSON in JavaScript
Building a tree structure from a flat array is a common task in JavaScript. When you have hierarchical data represented as a flat array with codes indicating parent-child relationships, you can transform it into a nested tree structure. The Problem Suppose we have the following flat array where the code property indicates hierarchy through dot notation: const arr = [{ "code": "2", "name": "PENDING" }, { "code": "2.2", "name": "PENDING CHILDREN" }, { "code": "2.2.01.01", ...
Read MoreFlattening a JSON object in JavaScript
Flattening a JSON object means converting a nested object structure into a single-level object where nested keys are represented using dot notation. This technique is useful for data processing, form handling, and API transformations. Suppose we have the following JSON object that may contain nesting up to any level: const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 ...
Read MoreHow to add an element to a JSON object using JavaScript?
In this article, you will understand how to add an element to a JSON object using JavaScript. JSON objects are key-value pairs separated by colons and surrounded by curly braces {}. JavaScript provides two main ways to add new properties: bracket notation and dot notation. Using Bracket Notation Bracket notation is useful when property names contain special characters or are stored in variables. var jsonObject = { members: { host: "hostName", viewers: ...
Read MoreAdding a unique id for each entry in JSON object in JavaScript
In this problem statement, our task is to add a unique id for every object entry in a JSON object with the help of JavaScript. We will use a loop and a variable to store the id for each object in the JSON object. Understanding the Problem Statement The problem statement is to write a function in JavaScript that adds a unique id to every item in the given JSON object. For example, if we have: let obj = { "entry1": {empName: "A", age: 25}, "entry2": {empName: "B", age: 30} ...
Read More