Vishali
Vishali

Reputation: 1397

How to parse dynamic key values from api response

Here is my response

{
    "status": 1,
    "msg": "success",
    "data": {
        "118377742": {
            "gid": 118377742,
            "employee_id": 1,
            "status": "Present",
            "intime": "2022-09-24 10:42:50"
        },
        "118377785": {
            "gid": 118377785,
            "employee_id": 2,
            "status": "Present",
            "intime": "2022-09-24 09:50:59"
        },
        "118373696": {
            "gid": 118373696,
            "employee_id": 4,
            "name": "Rajasekaran G",
            "status": "Leave",
            "intime": null
        },
        "118378064": {
            "gid": 118378064,
            "employee_id": 14,
            "status": "Leave",
            "intime": null
        },
        "118378229": {
            "gid": 118378229,
            "employee_id": 18,
            "status": "Leave",
            "intime": null
        },
        "118378271": {
            "gid": 118378271,
            "employee_id": 19,
            "status": "Leave",
            "intime": null
        },
    }
}

I am new to React Native application development and I have response from api like above. I want to store employee_id and staus values from this response.I don't what value will be coming under data object. How can I parse this JSON?

Upvotes: 0

Views: 89

Answers (1)

Anis R.
Anis R.

Reputation: 6902

One way is to loop over the values from the key-value pairs of the data. This way, even if we do not know the keys before hand, we can still access the values.

For example, if the response object is stored in a variable called res:

Object.values(res["data"]).forEach((employee_obj) => {
    // do something with employee_obj["employee_id"]
    // do something with employee_obj["status"]
})

Sample demo below:

var res = {
    "status": 1,
    "msg": "success",
    "data": {
        "118377742": {
            "gid": 118377742,
            "employee_id": 1,
            "status": "Present",
            "intime": "2022-09-24 10:42:50"
        },
        "118377785": {
            "gid": 118377785,
            "employee_id": 2,
            "status": "Present",
            "intime": "2022-09-24 09:50:59"
        },
        "118373696": {
            "gid": 118373696,
            "employee_id": 4,
            "name": "Rajasekaran G",
            "status": "Leave",
            "intime": null
        },
        "118378064": {
            "gid": 118378064,
            "employee_id": 14,
            "status": "Leave",
            "intime": null
        },
        "118378229": {
            "gid": 118378229,
            "employee_id": 18,
            "status": "Leave",
            "intime": null
        },
        "118378271": {
            "gid": 118378271,
            "employee_id": 19,
            "status": "Leave",
            "intime": null
        },
    }
};

// loop over employees
Object.values(res["data"]).forEach((employee_obj) => {
    // for eqach employee:
    // print employee ID of the current employee
    console.log(employee_obj["employee_id"]);
    
    // print status of the current employee
    console.log(employee_obj["status"]);
})

Upvotes: 1

Related Questions