Vishnu S Babu
Vishnu S Babu

Reputation: 1640

Convert a JSON Object to Comma Sepearted values in javascript

I have a JSON OBJECT

{"data":{"source1":"source1val","source2":"source2val"}}

which i want to convert into

data : source1val, source2val.

Upvotes: 2

Views: 1958

Answers (2)

SuperNova
SuperNova

Reputation: 27466

var input = {
  "data": {
    "source1": "source1val",
    "source2": "source2val"
  }
};
var output = [];
var i;
for (i = 0; i < input.data.length; i++) { 
    output.push(input.data[i]);
}

Upvotes: 0

Rayon
Rayon

Reputation: 36609

Use Object.keys with Array#map

The Object.keys() method returns an array of a given object's own enumerable properties.


The map() method creates a new array with the results of calling a provided function on every element in this array.

var input = {
  "data": {
    "source1": "source1val",
    "source2": "source2val"
  }
};
var output = Object.keys(input.data).map(function(k) {
  return input.data[k];
}).join(',');
console.log(output); //manipulated object
console.log(input); //Original object
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Upvotes: 3

Related Questions