6

i have a json. i need to copy the content of "data" if provider is "AXIS DATA" to a new json file. How?

what i have tried is

first i convert with json.parse and searching it with for loop.

this is my code so far

var hasilsearching = {};
var hasildata = JSON.parse( FIRST JSON );
for (var i=0 ; i < hasildata.data.length ; i++){
    if (hasildata.data[i].provider == "AXIS DATA") {
        hasilsearching = hasildata.data[i];
    }   
}

but not what i expected. I know inside the if() is the problem. but i dont know.

this is my 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"
}

this is expected new json file

{
    "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"
        }
    ]
}
1
  • 1
    hasilsearching = hasildata.data[i]; should be hasilsearching['data'][] = hasildata.data[i]; Commented Apr 24, 2019 at 6:42

4 Answers 4

2

Add an empty array to the data property of hasilsearching, and push any matches on:

var hasildata = {"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"};

var hasilsearching = { data: [] };
for (var i=0 ; i < hasildata.data.length ; i++){
  if (hasildata.data[i].provider == "AXIS DATA") {
    hasilsearching.data.push(hasildata.data[i]);
  }   
}

console.log(hasilsearching);
.as-console-wrapper { max-height: 100% !important; top: auto; }

It's also much easier to use filter:

var hasildata = {"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"};

var hasilsearching = { data: hasildata.data.filter(({ provider }) => provider == "AXIS DATA") };

console.log(hasilsearching);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the answer, its worked. Use filter is great.
hey, sorry for late response. When i use filter in answer above, i get a warning message. It says 'destructuring binding' is available in ES6 (use 'esversion: 6') and 'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6') how i get rid of this warning?
1

You can simply filter your data like that:

let filteredData = hasildata.data.filter(x => x.provider=='AXIS DATA');

Comments

1

Using the forEach loop

var data={
    "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"
}
var d={data:[]}
data.data.forEach(e=>{
if(e.provider=="AXIS DATA")
d.data.push(e)
})

Comments

0

if you want to push your fields to an array use the following :

var data = [];
for(var el of myjson.data){
  if(el.provider === "AXIS DATA"){    
    data.push(JSON.parse(JSON.stringify(el)));
  }
}

2 Comments

This won't work - myjson.data is an array of objects, not a single object. This is why some kind of loop is required.
@JackBashford thank you for pointing that, i updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.