Found 208 Articles for JSON

Flattening a JSON object in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:20:27

14K+ Views

Suppose, we have the following JSON object that may contain nesting upto any level −const obj = {    "one": 1,    "two": {       "three": 3    },    "four": {       "five": 5,       "six": {          "seven": 7       },       "eight": 8    },    "nine": 9 };We are required to write a JavaScript function that takes in one such nested JSON object and returns a new object that contains no nesting and maps the corresponding values to the keys using the dot ... Read More

Adding a unique id for each entry in JSON object in JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 10:46:42

4K+ Views

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. So for doing this task 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 by which we can add a unique id to every item in the given JSON object. For updating every JSON object by adding new id to every object we will use basic Javascript functionality. For example we ... Read More

Build tree array from JSON in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:25:34

3K+ Views

Suppose, we have the following array in JavaScript −const arr = [{    "code": "2",    "name": "PENDING" }, {    "code": "2.2",    "name": "PENDING CHILDREN" }, {    "code": "2.2.01.01",    "name": "PENDING CHILDREN CHILDREN" }, {    "code": "2.2.01.02",    "name": "PENDING CHILDREN CHILDREN02" }, {    "code": "1",    "name": "ACTIVE" }, {    "code": "1.1",    "name": "ACTIVE CHILDREN" }, {    "code": "1.1.01",    "name": "ACTIVE CHILDREN CHILDREN" }];We are required to write a JavaScript function that takes in one such array. The function should build a tree structure from this array based on ... Read More

Convert JSON array into normal json in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:16:19

743 Views

Suppose, we have a JSON array with key/value pair objects like this −const arr = [{    "key": "name",    "value": "john" }, {    "key": "number",    "value": "1234" }, {    "key": "price",    "value": [{       "item": [{          "item": [{             "key": "quantity",             "value": "20"          },          {             "key": "price",             "value": "200"          }]       }] ... Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
Updated on 23-Nov-2020 10:43:43

471 Views

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 −const output = {    "breakfast" : ["eggs", "yogurt", "toast"],    "lunch": ["falafel", "mushrooms", "fries"],    "dinner": ["pasta", "cheese"] };ExampleThe code for this will be −const meals = ["breakfast", "lunch", "dinner"]; const ingredients ... Read More

Get value for key from nested JSON object in JavaScript

Nikitasha Shrivastava
Updated on 23-Aug-2023 12:42:59

18K+ Views

In the given problem statement we are asked to get value for key from nested JSON object with the help of javascript functionalities. In Javascript we can solve and access the json data object with the help of dot notation or bracket notation. Before starting coding for the given problem, let's understand what exactly is the JSON object: What is JSON and nested JSON objects? JSON’s full form is Javascript Object Notation. It is a standard text based format to show the structured data. It is basically used to transmit data in web applications, which means we can send ... Read More

JSON group object in JavaScript

Nikitasha Shrivastava
Updated on 23-Aug-2023 14:34:43

775 Views

In the given problem statement we are asked to group the JSON object with the help of javascript functionalities. This problem can be solved with the help of a simple algorithm in javascript. What is JSON ? JSON (Javascript Object Notation) is lightweight data which can be transferred between two devices. This data can be read and written by humans. JSON objects are represented in the form of key−value pairs. Keys are strings to define values. In JSON each entry is separated by a semicolon. For example − {“Car” : “Audi”}, in this example Car is a key and Audi ... Read More

Calculate average from JSON data based on multiple filters JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:05:38

1K+ Views

Suppose, we have an array of objects like this −const arr = [    { "SupplierName" : "John", "Category " : "A", "Points" : 3 },    { "SupplierName" : "John", "Category " : "A", "Points" : 11 },    { "SupplierName" : "John", "Category " : "A", "Points" : undefined },    { "SupplierName" : "John", "Category " : "B", "Points" : 2 },    { "SupplierName" : "John", "Category " : "B", "Points" : 6 },    { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 },    { "SupplierName" : "Praveen", "Category " : "A", ... Read More

Group Similar Items in JSON in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:02:26

2K+ Views

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": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to ... Read More

Search by id and remove object from JSON array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:55:56

3K+ Views

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 a JavaScript function that takes in one such array as the first argument and an id string as the second argument. Then our function should search for the object ... Read More

Advertisements