Akshay Talathi
Akshay Talathi

Reputation: 83

JavaScript - Check if a particular key is available in the JSON

I am retrieving JSON from a live streaming data. For the first call I am getting dataset array with time and value. But in the second JSON dataset array is empty. I want to check if dataset array contains time key.

Retrieved JSON after first call:

 {
  "activities-heart-intraday": {
    "dataset": [{
        "time": "00:00:00",
        "value": 91
    }, {
        "time": "00:01:00",
        "value": 92
    }, {
        "time": "00:02:00",
        "value": 92
    }],
    "datasetInterval": 1,
    "datasetType": "second"
  }
}

Retrieved JSON after second call:

{
  "activities-heart-intraday": {
    "dataset": [],
    "datasetInterval": 1,
    "datasetType": "second"
  }
}

I am doing

var value = JSON.parse(data);

if (value.hasOwnProperty('time')) {
   console.log("here");
} 

to check if time key exists in the JSON, but it's not working.

How can I check if a particular key exists in the array in json?

Upvotes: 2

Views: 12553

Answers (4)

Lee Shau
Lee Shau

Reputation: 11

I can't really tell what data from your question is supposed to be but if it is the whole JSON object, then the most simple way is this:

if(data["activities-heart-intraday"]["dataset"][0]["time"])
  console.log('time is set)

But beware! If for example the dataset is not set, you'll get an error that you are trying to get time key from undefined and the code will crash. I'd recommend using simple recursive function like this:

function is_there_the_key(json, keys){
  if(keys.length == 0)
    return true           //we used the whole array, and every key was found
  let key = keys.shift()  //pop(get and remove) the first string from keys
  if(!json[key]){
    console.log(key + ' not found')
    return false          //one of the keys doesn't exist there
  }
  return is_there_the_key(json[key], keys)
}

Whether you return true or false, both of them will make their way up to the surface.

As the json parameter you pass the json you want to search in.

As the keys parameter you pass an array (mostly strings) of the keys in order they should go. For example:

if(is_there_the_key(data, ["activities-heart-intraday", "dataset", 0, "time"])
  //we found the key, do something here

Upvotes: 0

Redu
Redu

Reputation: 26201

Since in JS you can not natively set and access object properties those with a "-" character in it by dot notation you have define them as strings and use bracket notation instead to set and access them. So you can make a check like this

data["activities-heart-intraday"].dataset.length > 0 && data["activities-heart-intraday"].dataset.every(e => !!e.time);

Upvotes: 0

GG.
GG.

Reputation: 21904

Firstly you have to check that dataset is not an empty array. Then check that time is defined.

This can be solved with:

if (dataset[0] !== undefined && dataset[0].time !== undefined)

or just:

if (dataset[0] && dataset[0].time)

If you want to iterate through the array:

dataset.forEach(function (data) {
   if (data.time) {
       // your code
   } 
});

Upvotes: 3

Marcin Strzyż
Marcin Strzyż

Reputation: 61

the data has a dataset array so we need to first check if the array is there and then if one of the arrays members has the time property

if( data.hasOwnProperty('dataset') && data.dataset.length != 0){    
  if( data.dataset[0].hasOwnProperty('time')){
    console.log('true');    
  }
}

Upvotes: 0

Related Questions