Reputation: 313
I want to transform a JSON formatted output to another. How I can do this?
Example: Old JSON
"data":
[
{
"id" : "e49e183e-9325-4e62-8eda-7e63fb7cdbbd",
"name" : "test"
},
{
"id" : "ac310894-d808-447b-a189-d07edb7f6dd7",
"name" : "test2"
}
]
New JSON which I want without braces only like this with bracket
"aaData":
[
[
"e49e183e-9325-4e62-8eda-7e63fb7cdbbd","test"
],
[
"ac310894-d808-447b-a189-d07edb7f6dd7","test2"
]
]
Upvotes: 5
Views: 30622
Reputation: 13639
JSONata in the right way !
Expresion:
{ "aaData": data.[id, name] }
Result:
{
"aaData": [
[
"e49e183e-9325-4e62-8eda-7e63fb7cdbbd",
"test"
],
[
"ac310894-d808-447b-a189-d07edb7f6dd7",
"test2"
]
]
}
Run it on: https://try.jsonata.org/Pg51Kzp0c
Upvotes: 2
Reputation: 2441
Just another answer using JSONata library:
What you want is easily achievable using the following:
$.[id, name]
For live demo look at: https://try.jsonata.org/UXYx6_xEh
Upvotes: 4
Reputation: 30002
You could just loop through the items and push them into a new object:
var len = old.data.length,
newData = {aaData:[]},
i;
for ( i=0; i < len; i+=1 ) {
newData.aaData.push( [ old.data[ i ].id, old.data[ i ].name] );
}
example: https://jsfiddle.net/q2Jzb/1/
You are presumably passing these to DataTables (as you use the name aaData), note that DataTables takes an object as the configuration, it's not the same as JSON.
Upvotes: 10
Reputation: 53
You can also use JSON Path and use the query ($..id) on your dataset yields exactly what you seem to be looking for. Checkout https://jsonpath.com/? and enter your data in the left hand box and $..id in the text box for JSONPath Syntax
Upvotes: 2
Reputation: 150010
You could just do it as a string replace:
newJSON = oldJSON.replace(/"id" : |"name" : /g, "")
.replace(/{/g, "[").replace(/}/g, "]");
But that's kind of dodgy. The other way is to parse the JSON and then process the resulting object and then turn the result back into JSON:
var oldData = JSON.parse(oldJSON)["data"],
aaData = [],
arr,
newJSON,
i, k;
for (i = 0; i < data.length; i++) {
arr = [];
for (k in data[i])
arr.push(data[i][k]);
aaData.push(arr);
}
newJSON = JSON.stringify({ "aaData" : aaData });
Where oldJSON
is the string with the old format from your question. (I've just looped through the properties in each array item rather than assuming they'll always have "id" and "name" properties.)
Upvotes: 0
Reputation: 2135
You can use JavaScript to transform the input data into a new form of object and then use JSON JavaScript library (the JSON.stringify function, see more here http://www.json.org/js.html) to convert the newly created object into a proper JSON. The following code uses jQuery and the JSON library to solve your problem. Use of jQuery is purely optional, as well as there are other libraries to make JSON.
<pre id="code"></pre>
<script type="text/javascript">
$(document).ready(function () {
var old = { "data":
[
{
"id": "e49e183e-9325-4e62-8eda-7e63fb7cdbbd",
"name": "test"
},
{
"id": "ac310894-d808-447b-a189-d07edb7f6dd7",
"name": "test2"
}
]
};
var newData = [];
for (var i = 0, l = old.data.length; i < l; i++) {
var o = old.data[i];
newData[i] = [o.id, o.name];
}
$("#code").text(JSON.stringify(newData));
});
</script>
Upvotes: 1
Reputation: 75307
Try the following;
function format(oldFormat) {
var newFormat = [];
oldFormat = oldFormat.data;
for (var i=0;i<oldFormat.length;i++) {
newFormat.push([oldFormat[i].id], oldFormat[i].name);
};
return {
aaData: newFormat
};
}
You'd then call use the function by;
var newStuff = format(varPointingToOldStuff);
The function expects to receive a JavaScript object rather than JSON, and returns a JavaScript object rather than JSON. Make sure you understand the differences between a JSON (string) and a JavaScript object.
You can convert a JSON string to a JavaScript object using JSON.parse(yourJsonString)
, and can convert a JavaScript object to a JSON string using JSON.stringify(yourJavaScriptObject)
.
Upvotes: 1