0

I have an API response coming in this format.

 [{
 "response_data": {
    "0":{
    "id" : 0,
    "office" : "India",
    "type" : 'Perm'
    },
    "1":{
    id : 0,
    "office" : "America",
    "type" : 'Perm'
    },
    "2":{
    id : 0,
    "office" : "Europe",
    "type" : 'Contract'
    },
    "2":{
    id : 0,
    "office" : "Latin America",
    "type" : 'Contract'
    }

    }}]

I am trying to get all the office where the type is Contract. I have the json response saved in a variable like - using Chakram as

var response_json = t.body[0].response_data;

which gives me correct response in the console.log as

       "0":{
    "id" : 0,
    "office" : "India",
    "type" : 'Perm'
    },
    "1":{
    id : 0,
    "office" : "America",
    "type" : 'Perm'
    },
    "2":{
    id : 0,
    "office" : "Europe",
    "type" : 'Contract'
    },
    "2":{
    id : 0,
    "office" : "Latin America",
    "type" : 'Contract'
    }

Now how to get to the corresponding keys in inside the json and extract the information required. I tried this but it returns undefined

var length = Object.keys(response_json).length;
for(var i = 0; i<= length;i++){
console.log(response_json.i) //returns undefined
 console.log((Object.keys(response_json)).id); //returns undefined.
}

I know that this can be solved using filter method if the response was an array, but how can I do the same operation in JSON object? I am looking for an optimized solution because the API returns almost 5000 objects. If this question has already been asked, provide reference since I was not able to find any on SO.

demouser123
  • 4,108
  • 9
  • 50
  • 82
  • 1
    That response is not valid JSON - all property names must be enclosed within double quotes, as well as all string values. – phuzi Jul 18 '18 at 11:25

4 Answers4

1

If you want to do this with filter method then a workaround would be to add a property length then using Array.from like below. Then you can use Array.prototype.filter method.

let o = {
    '0': {
        id: 0,
        "office": "India",
        "type": 'Perm'
    },
    '1': {
        id: 0,
        "office": "America",
        "type": 'Perm'
    },
    '2': {
        id: 0,
        "office": "Europe",
        "type": 'Contract'
    }
};

o.length = Object.keys(o).length;
let a = Array.from(o);

let r = a.filter(({ type }) => type == 'Contract');
console.log(r);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
0

Two major mistakes your code have one using loop up to the length of an array where index starts from 0. and second accessing an object from an array you use brackets instead of the dot. So update the code as follows:

var keys = Object.keys(response_json);
var length = keys .length;
for(var i = 0; i< length;i++){
   console.log(response_json[keys[i]]);
}
Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
0

On your response_json '0' , '1' all keys are in string format. But in your for loop 'i' is integer so key is unavailable. just convert it to string you can get desired result like console.log(response_json[''+i])

pranay das
  • 27
  • 1
  • 8
0
 var data =  [{"response_data": {
    '0':{id : 0, "office" : "India", "type" : 'Perm'},
    '1':{id : 0, "office" : "America","type" : 'Perm'},
    '2':{ id : 0, "office" : "Europe","type" : 'Contract'},
    '3':{ id : 0, "office" : "Latin America", "type" : 'Contract'}
    }}];

 var list = data[0]['response_data'];
    var filterList = [];
    for(var i in list) {
        if(list.hasOwnProperty(i)) {
            var type = list[i]['type'];
            if(type === 'Contract') {
                filterList.push(list[i]);
            }
        }
    }

May be not better in javascript if the record is more than 5 thousand, better process it on server side.

Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20