creating JSON with few columns from another JSON - json

I have a JSON file like following:
[{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9107,"value":24.0,"Children59m":0.0},
{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9110,"value":92.0,"Children59m":8.0},
{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9111,"value":78.0,"Children59m":4.0}]
I would like to create another JSON with only three columns like following from the above JSON object.
[{"Pname":"Kabul","Time":9107,"value":24.0},
{"Pname":"Kabul","Time":9110,"value":92.0},
{"Pname":"Kabul","Time":9111,"value":78.0}]
Anybody, please help.

You can use the for loop
UPDATED
var json = [{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9107,"value":24.0,"Children59m":0.0},{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9110,"value":92.0,"Children59m":8.0},{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9111,"value":78.0,"Children59m":4.0}]
for(var i in json)
{
delete json[i]['PROV_32_ID'];
json[i]['value'] = json[i]['Children59m'];
delete json[i]['Children59m'];
}
console.log(json)

Related

How can I get data from JSON Array in ExtJS?

I can't get value from Json Array in ExtJS
Example for my code:
var array = [];
array.push({"id": data.id, "name": data.name});
I tried below codes but I couldn't get it. How should I do?
array[0].data.data.id;
array[0].data.id;
array[0].data.data.get('id');
I solved it with this code:
array.items[0].data.id;

Set Postman variable based on a json without tag

I'm pretty new with using postman and I'm trying to store one part of the JSON response of my request in a variable. My problem is that the JSON doesn't have tags. It looks like this:
[
"01000511",
"00000001",
"00000009",
"01000654"
]
Is it possible to store the JSON as an array and create variables like this: id1=response[0], id2=response[1]?
Thanks in advance!
Assuming the response is just like you described, you can do this using the "Tests" tab in your Postman request.
Here is the code
// If the response is exactly like you described (You may have to adapt the query to get your array)
const resp = pm.response.json()
for (var i = 0;i<resp.length;i++) {
pm.environment.set("id"+i, resp[i]);
}
This result like this in your Env catalog:

Node.js looping with json

I'm looking for the simplest way to loop throug a JSON file.
The Data Syntax (can't change that):
{"1":{"name":FOO","price":"1","sold":"100"},"2":{"name":"FOO","price":"1","sold":"100"}
The data is stored i a file called prices.json. How can I loop through all 7573 entrys?
Thanks..
You could simply require the json file then iterate over the properties of the object that it contains.
var prices = require('./prices.json');
for (var i in prices) {
if (prices.hasOwnProperty(i)) {
console.log(prices[i]); // do something with each item...
}
}

How to format a json string and show result in table?

I am using CakePhp _serialize method to make a web service and show the data in JSON format.I used the .json extension at the end of the URL to show this data.Now i want to show this data in table.Output image is attached.Is this possible then how i can do it?
Thanks
The format is a bit odd. I would prefer something like: "task_list": [ .... ]; iterating over objects is always a bit tedious.
Here is the jQuery code:
var data = ...;
var items = data["task_list"];
var table = $('<table/>');
table.appendTo($('body'));
$.each(items, function(id, value) {
var tr = $('<tr/>');
table.append(tr);
$('<td/>').text(id).appendTo(tr);
$('<td/>').text(value).appendTo(tr);
});

jQuery template issue with JSON data

I'm using jQuery templating which is taking JSON data to create a table. This code is working fine. But when I try to create a function around this code and pass JSON data to it which I'm getting from an AJAX request, it shows the values in the console but not in the table. Working code with dummy data is:
var json = [{"class":12,"marks":"500","marks1":"200","marks2":"300"},{"class":11,"marks":"200","marks1":"300","marks2":"400"}]
$.template('kList','<tr title="${class}"><td>${marks}</td><td>${marks1}</td><td>${marks2}</td></tr>');
for(var i=0; i<json.length; i++){
$.tmpl('kList',json[i]).appendTo("#table1")
}
Here is the code where I'm warping the upper code in function and passing the JSON data as a parameter that shows the values in console when I print it with console.log(json) but not filling the table. The JSON parameter is having the same JSON data as in above code.
function dataTable(json){
console.log(json); // here json values are appearing in the console
$.template('kList','<tr title="${class}"><td>${marks}</td><td>${marks1}</td><td>${marks2}</td></tr>');
for(var i=0; i<json.length; i++){
$.tmpl('kList',json[i]).appendTo("#table1")
}
}
Please help me out because I don't know whats wrong in this code. Thanks in advance.
Your json parameter is a string. You should convert it to an object, using $.parseJSON(json).
Take a look at this for conversion detail.