I have a JSON file and I need to extract the content of "data" where the provider is "AXIS DATA" into a new JSON file. How can this be achieved?
Here's what I've attempted:
First, I convert it using JSON.parse
and then search for the desired data with a for loop.
This is the code I have so far:
var searchedResult = {};
var jsonData = JSON.parse( YOUR_JSON_DATA );
for (var i=0; i < jsonData.data.length; i++) {
if (jsonData.data[i].provider == "AXIS DATA") {
searchedResult = jsonData.data[i];
}
}
However, the result was not as expected. I believe the issue lies within the if()
condition but I am unsure about the exact problem.
This is the sample JSON file:
{
"errNumber": "0",
"userID": "EKL0003097",
"data": [
{
"code": "BXD1",
"price": "15000.00",
"name": "Voucher Axis Aigo 1GB 24J 30H",
"ep": "770",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BXD2",
"price": "25000.00",
"name": "Voucher Axis Aigo 2GB 24J 30H",
"ep": "660",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BOLT1",
"price": "31000.00",
"name": "Bolt Kuota 1,5GB 24Jam 30hr",
"ep": "1320",
"isActive": "Active",
"type": "KUOTA",
"provider": "BOLT"
},
{
"code": "BOLT3",
"price": "50000.00",
"name": "Bolt Kuota 3GB 24Jam 30hr",
"ep": "1127",
"isActive": "Active",
"type": "KUOTA",
"provider": "BOLT"
}
],
"respMessage": "PROSES BERHASIL"
}
The expected output in the new JSON file should be:
{
"data": [
{
"code": "BXD1",
"price": "15000.00",
"name": "Voucher Axis Aigo 1GB 24J 30H",
"ep": "770",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
},
{
"code": "BXD2",
"price": "25000.00",
"name": "Voucher Axis Aigo 2GB 24J 30H",
"ep": "660",
"isActive": "Active",
"type": "KUOTA",
"provider": "AXIS DATA"
}
]
}