Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple key values in JSON object?

I have this sample JSON object

var sample = [{
    "label": "one",
    "value": 1
}, {
    "label": "two",
    "value": 2
}, {
    "label": "three",
    "value": 3
}, {
    "label": "four",
    "value": 4
}, {
    "label": "five",
    "value": 5
}];

I want to change it some thing like this

var sample = [{
    "label": "one",
    "value": 1,
    "newKeyValue": "one|1"
}, {
    "label": "two",
    "value": 2,
    "newKeyValue": "two|2"
}, {
    "label": "three",
    "value": 3,
    "newKeyValue": "three|3"
},
...
];

It should combine both key values and return new key value combining both.

JSON is coming dynamically key label and value are not static it can be anything. For example [{"name":"srinivas","lastname":"pai"}]

like image 419
Shrinivas Pai Avatar asked Jan 19 '16 11:01

Shrinivas Pai


People also ask

Can JSON have multiple keys?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.

How do I combine multiple JSON objects into one?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util.

How do I get key-value pairs in JSON?

In order to get a key-value pair from a KiiObject, call the get() method of the KiiObject class. Specify the key for the value to get as the argument of the get() method. The value of the key at the first level of the JSON document hierarchy will be obtained.

What is key-value pair in JSON?

JSON object literals contains key/value pairs. Keys and values are separated by a colon. Keys must be strings, and values must be a valid JSON data type: string. number.


2 Answers

You can use map like this :

EDIT

For handling generic keys you can use Object.keys(d)[0] for first key Object.keys(d)[1] for second key

var sample = [

     {
      "label":"one",
      "value":1
     },

     {
      "label":"two",
      "value":2
     },
     {
      "label":"three",
      "value":3
     },
     { 
      "label":"four",
      "value":4
     },
     { 
      "label":"five",
      "value":5
      }

     ];
    var data = sample.map(function(d){
     return {label: Object.keys(d)[0], value: Object.keys(d)[1], newKeyValue:  Object.keys(d)[0] +"|" + Object.keys(d)[1]}
    }) 
    console.log(data)

Hope this helps!

like image 147
Cyril Cherian Avatar answered Oct 30 '22 08:10

Cyril Cherian


You can use Array#map(), Object.keys(), and Array#join().

In ES6, you can use Arrow functions.

sample = sample.map(obj => {
    var keys = Object.keys(obj);
    obj.newKeyValue = keys.map(key => obj[key]).join('|');
    return obj;
});

var sample = [{
    "label": "one",
    "value": 1
}, {
    "name": "two",
    "age": 2
}, {
    "five": "three",
    "six": 3
}, {
    "company": "four",
    "organization": 4
}, {
    "label": "five",
    "value": 5
}];

sample = sample.map(function (x) {
    var keys = Object.keys(x);
    x.newKeyValue = keys.map(key => x[key]).join('|');
    return x;
});
console.log(sample);
document.body.innerHTML = '<pre>' + JSON.stringify(sample, 0, 4) + '</pre>';

In ES5, you can use the same code with anonymous functions

sample = sample.map(function (obj) {
    var keys = Object.keys(obj);
    obj.newKeyValue = keys.map(function (key) {
        return obj[key]
    }).join('|');
    return obj;
});

Limitations due to dynamic keys:

  1. The order of the keys in object cannot be maintained
  2. This will join all the available keys in the object (in case if you just want to join fewer)
like image 40
Tushar Avatar answered Oct 30 '22 08:10

Tushar