0

Hi I have a json string

"{"data":[{"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":15,"ConfigName":"Offline Days","ConfigValue":"Sunday"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}]}"

I want to check ConfigValue of ConfigName "CurrentTime". Currently I am accessing it by below code

var d = JSON.parse(data).data;
d[2].ConfigValue

but sometimes the json string will be

"{"data":[{"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}]}"

according to above string now if i want to access "CurrentTime"

I will have to write below code

 var d = JSON.parse(data).data;
    d[1].ConfigValue

So can anyone tell how to access it? Because the array may change anytime so I cannot hardcode the array index like that.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Anuj
  • 1,496
  • 1
  • 18
  • 28
  • Please use the search: [`[javascript] find object array`](http://stackoverflow.com/search?q=%5Bjavascript%5D+find+object+array) – Felix Kling Dec 23 '16 at 06:41

4 Answers4

1

You have same quotes inside and outside string without escaping. But maybe it takes place only in question texts.

In this case You need to check every item like this:

 for (var i in d) {
   if (d[i].Id == 0) {
       alert(d[i].ConfigName);
   }
 }
MadDocNC
  • 670
  • 5
  • 17
1

You can run a loop and check the value of ConfigName. Below is the code

var data = '{"data":[{"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":15,"ConfigName":"Offline Days","ConfigValue":"Sunday"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}]}';
var d = JSON.parse(data).data;

for(var i=0; i<d.length; i++)
{
if(d[i].ConfigName === 'CurrentTime')
  {
   alert(d[i].ConfigValue); //Do stuff with the value.
  }
}
karan3112
  • 1,867
  • 14
  • 20
0

You can try this:

var k = {"data":[{"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":15,"ConfigName":"Offline Days","ConfigValue":"Sunday"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}]}
    
    var result = $(jQuery.parseJSON(JSON.stringify(k))).each(function() {  
         var configName = this.ConfigName;
         var configValue = this.ConfigValue;
         
    });
 console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 1
    Why do you use jQuery here? And why do you convert the object to JSON just to parse it again? Seems more complicated than it needs to be. – Felix Kling Dec 23 '16 at 06:43
0

You first need to find the object in the array with "ConfigValue" equal to "CurrentTime". Once you have this object, you can simply access its "CongigValue" property.

var json1 = {"data":
             [
               {"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":15,"ConfigName":"Offline Days","ConfigValue":"Sunday"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}
             ]
            };

var json2 = {"data":
             [
               {"Id":14,"ConfigName":"Online Hrs","ConfigValue":"00:01-23:59"},{"Id":0,"ConfigName":"CurrentTime","ConfigValue":"11:3"}
             ]
            };


var currentTime = findCurrentTime( json1 );
console.log( "currentTime: " + currentTime );

var currentTime = findCurrentTime( json2 );
console.log( "currentTime: " + currentTime );



function findCurrentTime( jsonObj )
{
  var filteredArray = jsonObj.data.filter(function(element){
     return element.ConfigName === "CurrentTime";
  });//filter()

  if( filteredArray.length > 0 )
    return filteredArray[0].ConfigValue;
  
  return false;
}//findCurrentTime()
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64