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.

1

4 Answers 4

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.
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

Relying on the fact the value only contains a single : is so hacky. Why not just look for the object where ConfigName === 'CurrentTime' ?
@FelixKling yes you are right. My bad.
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);
   }
 }

Comments

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>

1 Comment

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.
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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.