Found 208 Articles for JSON

Compare keys & values in a JSON object when one object has extra keys in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:41:17

2K+ Views

Suppose, we have two JSON objects like these −const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};We are required to write a JavaScript function that takes in two such objects. We want to be able to have a Boolean check comparing the two objects without having to remove data from either one.For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.However, let’s say obj1 stays the same but obj2 ... Read More

Transform tree from DB format to JSON format in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:47:22

454 Views

Suppose, we have an array of objects like this −const arr = [    {"id":7, "name":"Kuwait", "parentId":2},    {"id":4, "name":"Iraq", "parentId":2},     {"id":10, "name":"Qatar", "parentId":2},    {"id":2, "name":"Middle East", "parentId":1},    {"id":3, "name":"Bahrain", "parentId":2},    {"id":6, "name":"Jordan", "parentId":2},    {"id":8, "name":"Lebanon", "parentId":2},    {"id":1, "name":"Africa/Middle East", "parentId":null},       {"id":5, "name":"Israel", "parentId":2},     {"id":9, "name":"Oman", "parentId":2} ];We are required to write a JavaScript function that takes in one such array. The function should then prepare a new array that contains the objects grouped according to their parents.Therefore, the output should look like this −const output = [ ... Read More

Create array from JSON object JavaScript

Alshifa Hasnain
Updated on 20-Jan-2025 19:19:41

1K+ Views

In this article, we will learn to create an array from JSON data in JavaScript. JSON is a widely used format for storing and exchanging data. A common use case involves extracting specific data from a JSON object or array and converting it into a separate array for further processing. What is JSON Data? JSON (JavaScript Object Notation) is a lightweight data format commonly used to store and exchange data between a server and a client. It represents data as key-value pairs and is easy for both humans and machines to read and write. In JavaScript, JSON objects or arrays can be ... Read More

Merge JSON array date based JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:16:25

716 Views

Suppose, we have the following array of objects −const arr = [    {       "date" : "2010-01-01",       "price" : 30    },    {       "date" : "2010-02-01",       "price" : 40    },    {       "date" : "2010-03-01",       "price" : 50    },    {       "date" : "2010-01-01",       "price2" : 45    },    {       "date" : "2010-05-01",       "price2" : 40    },    {       "date" : "2010-10-01", ... Read More

Convert JSON to another JSON format with recursion JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:42:20

2K+ Views

Suppose, we have the following JSON object −const obj = {    "context": {       "device": {          "localeCountryCode": "AX",          "datetime": "3047-09-29T07:09:52.498Z"       },       "currentLocation": {          "country": "KM",          "lon": -78789486,       }    } };We are required to write a JavaScript recursive function that initially takes in one such array.The function should split the above object into a "label" - "children" format.Therefore, the output for the above object should look like −const output = {   ... Read More

Deep Search JSON Object JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:38:35

6K+ Views

Suppose we have the following nested JSON object −const obj = {    id: 1,    title: 'hello world',    child: {       id: null,       title: 'foobar',       child: {          id: null,          title: 'i should be in results array '       }    },    foo: {       id: null,       title: 'i should be in results array too!' },       deep: [       {          id: null,         ... Read More

How to turn a JSON object into a JavaScript array in JavaScript ?

AmitDiwan
Updated on 20-Nov-2020 09:59:28

589 Views

Suppose, we have this JSON object where index keys are mapped to some literals −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" };We are required to write a JavaScript function that takes in one such object and uses the object values to construct an array of literals.ExampleThe code for this will be −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" }; const objectToArray = (obj) => {    const res = [];    const keys = Object.keys(obj);    keys.forEach(el => {       res[+el] = obj[el];    });    return res; }; console.log(objectToArray(obj));OutputAnd the output in the console will be −[ 'Rakesh', 'Dinesh', 'Mohit', 'Rajan', 'Ashish' ]

Finding the smallest value in a JSON object in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:57:09

841 Views

We are required to write a JavaScript function that takes in a JSON object as one and only argument.The JSON object has string keys mapped to some numbers. Our function should traverse through the object, find and return the smallest value from the object.ExampleThe code for this will be −const obj = {    "a": 4,    "b": 2,    "c": 5,    "d": 1,    "e": 3 }; const findSmallestValue = obj => {    const smallest = Object.keys(obj).reduce((acc, val) => {       return Math.min(acc, obj[val]);    }, Infinity);    return smallest; } console.log(findSmallestValue(obj));OutputAnd the output in the console will be −1

Read by key and parse as JSON in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:52:57

251 Views

Suppose, we have a JSON array like this −const arr = [{    "data": [       { "W": 1, "A1": "123" },       { "W": 1, "A1": "456" },       { "W": 2, "A1": "4578" },       { "W": 2, "A1": "2423" },       { "W": 2, "A1": "2432" },       { "W": 2, "A1": "24324" }    ] }];We are required to write a JavaScript function that takes in one such array and converts it to the following JSON array −[    {       "1": ... Read More

How to convert JSON text to JavaScript JSON object?

AmitDiwan
Updated on 16-Jul-2020 06:57:01

854 Views

The JSON parse() method is used for converting a JSON text to a JavaScript object.Following is the code for converting JSON text to JavaScript JSON object −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 18px;       font-weight: 500;       color: red;    } JSON parse() Method {"name":"Rohan", "sports":["Cricket", "Football"], "country":"India"} CLICK HERE Click on the above button to parse the above JSON string    let ... Read More

Previous 1 ... 3 4 5 6 7 ... 21 Next
Advertisements