Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through a JSON object and return a key based on a specified value

After getting results from my api call with postman I was trying to get the corresponding id of the value "KC Content Kind ID" in my success function below, but since its not an array I was wondering if each will work in jquery and how to go about it. I want to loop through this nested json object by going into the facet object and say if the name attribute is "KC Content Kind ID" then return the id for that corresponding name attribute

    "results": {
       "data": {
          "facets": {
              "60749428": {
                "id": 60749428,
                "name": "KC Content Content Kind"
              },
              "60750276": {
                "id": 60750276,
                "name": "KC Content Product Version"
              },
              "69107204": {
               "id": 69107204,
               "name": "KC Video Audience"
              },
              "69127027": {
               "id": 69127027,
               "name": "KC Content Kind ID"
             }
          }
       }
    }

This is my code and I am referring to the success function

function getAvailableKinds() {
$.ajax({
    url: csexe + "/api/v2/facets/" +getLocationId(),
    dataType: "json",
    type: "GET",
    beforeSend: function(xhr) {
        xhr.setRequestHeader ("OTCSticket", getAuthToken());
    },
    success: function(response) {
        var obj = response.results.data.facets;
        $.each(obj, function(item, value){
             if ( value.name == 'KC Content Kind ID') {
                 var idRequired = obj.id;
             }
        });
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert("An error occurred... Look at the console");
        $("body").html('<p>status code: '+jqXHR.status+'</p><p>Error Thrown: ' + errorThrown + '</p><p>Response Text:</p><div>'+jqXHR.responseText + '</div>');
    }
});
like image 899
Tope Avatar asked Jun 29 '26 06:06

Tope


1 Answers

You can use Object.keys() and then filter the results based on your test. For example:

let results = {"data": {"facets": {"60749428": {"id": 60749428,"name": "KC Content Content Kind"},"60750276": {"id": 60750276,"name": "KC Content Product Version"},"69107204": {"id": 69107204,"name": "KC Video Audience"},"69127027": {"id": 69127027,"name": "KC Content Kind ID"}}}}

let obj = results.data.facets;
let k = Object.keys(obj).filter(key => obj[key].name === "KC Content Kind ID")

// an array of all matches
console.log(k)

If you know there will be only one, find() will find the first match:

let results = {"data": {"facets": {"60749428": {"id": 60749428,"name": "KC Content Content Kind"},"60750276": {"id": 60750276,"name": "KC Content Product Version"},"69107204": {"id": 69107204,"name": "KC Video Audience"},"69127027": {"id": 69127027,"name": "KC Content Kind ID"}}}}

let obj = results.data.facets;
let k = Object.keys(obj).find(key => obj[key].name === "KC Content Kind ID")

// the first match
console.log(k)
like image 143
Mark Avatar answered Jun 30 '26 20:06

Mark