0

I have a JSON structure like :

{
    listOfVal: [
        val1,
        val2,
        val3
    ]
    name: "app"
    id: "9209629"
}

I want to check if listOfVal has say val2. val2 can be at any index.

This JSON is part of a response to an API call and I am using:

var data = JSON.parse(responseBody);

to hold the JSON data.

3
  • that JSON is not valid. Commented Jul 11, 2014 at 0:57
  • @TMcKeown He probably just left the , symbols off, if he is successfully able to parse it, it's irrelevant to the question itself. Those val#s should be in quotes though, as far as I can tell. Commented Jul 11, 2014 at 0:59
  • thanks for the edit. I copied JSON from a JSON viewer and it had some formatting elements which i should have removed. My Bad. Commented Jul 11, 2014 at 1:01

2 Answers 2

3

First of all, your JSON isn't valid. You're missing commas and quotes.

{
    "listOfVal": [
        val1,
        val2,
        val3
    ],
    "name": "app",
    "id": "9209629",
    ...
}

once you have data, iterate over it with a for loop. You could use indexOf, but that doesn't work in IE < 9

function isInJSON(data, value){
    for(var i=0; i<data.listOfVal.length; i++){
        if(data.listOfVal[i] === value){
            return true;
        }
    }
    return false;
}

This won't always work if your vals are arrays or objects. If they are, you could run for loops to compare them, or you could use underscore.js: _.isEqual(obj1,obj2).

OP requested using forEach:

var value; //define what you're looking for
data.listOfVal.forEach(function(val,key){
    if(val === value){
        alert('MATCH FOUND: responseBody["' + String(key) + '"] = ' + String(value));
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

You actually don't need to include quotes around the keys within javascript objects-- it's implied, so whatever you use as the key names will automatically be converted into strings. Ah, good catch with indexOf... I always forget that nothing is compatible with IE < 9, even if it is specified in ECMAScript... hahaha
@Mercury You're right if the object was a javascript object, but JSON object have to have strings for keys. And yeah, compatibility for IE is always a cute joke haha
actually for(var i=0; i<data.listOfVal.length; i++) loop worked for me. Though i was looking for a solution like foreach. Is there a way to convert this into foreach as well for search by a value ?
-2

If after using JSON.parse you had an object like this:

data = {
    listOfVal: [ 
        val1, 
        val2, 
        val3 
    ],
    name: 'app',
    id: '9209629'
    ...
}

Then you can access listOfVal as if it were any other object, and use indexOf to try to reach val2.

data.listOfVal.indexOf(val2);

This will either return a non-negative integer index where val2 resides in the array, or it will return -1, indicating that val2 does not exist within your array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.