5

Is there is any way to check if the json string has the value(char or string)? Here is the example:

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    }
}

I have to check if this json has "m". It must know that "m" exists in a value.

6
  • and this isn't a string, it's an object (aside from the syntax errors) Commented Aug 21, 2013 at 17:12
  • now it is - but you mean you want to see if there is a value for the key "m"? Commented Aug 21, 2013 at 17:14
  • i ment, it serach 'm', in value not in key Commented Aug 21, 2013 at 17:16
  • A string looks like: var foo = '{"firstName": "John"}'; Commented Aug 21, 2013 at 17:16
  • This post may have some ideas for you: Search a JavaScript object Commented Aug 21, 2013 at 17:17

5 Answers 5

9

use this method, if you have json string, you can use json = $.parseJSON(jsonStr) to parse -

function checkForValue(json, value) {
    for (key in json) {
        if (typeof (json[key]) === "object") {
            return checkForValue(json[key], value);
        } else if (json[key] === value) {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You never check any keys after an embedded object! You should only return if the recursive call is true.
its getting checked in subsequent recursive calls
5

Assuming that the JSON object is assigned to var user

if(JSON.stringify(user).indexOf('m') > -1){  }

Sorry, upon reading new comments I see you're only looking to see if the string is in a key only. I thought you were looking for an 'm' in the entire JSON (as a string)

Comments

2

If looking in one layer and not a substring:

const hasValue = Object.values(obj).includes("bar");

If looking in one layer for a substring, and no objects as values:

const hasChar = Object.values(obj).join("").includes("m");

If looking in multi-layer for a substring:

const list = Object.values(a);
for (let i = 0; i < list.length; i++) {
    const object = list[i];
    if (typeof object === "object") {
        list.splice(i, 1); // Remove object from array
        list = list.concat(Object.values(object)); // Add contents to array
    }
}
// It is important to join by character not in the search substring
const hasValue = list.join("_").includes("m");

NOTE: If searching for a key instead, check this post

Comments

1

Assuming you get your object syntax corrected, you can loop through the properties in an object by using a for loop:

for(props in myObj) {
    if(myObj[props] === "m") { doSomething(); }
}

1 Comment

Which would fail in the case where the search value is in an embedded object.
1

Possibly something like this?

function parse_json(the_json, char_to_check_for)
{
    try {
        for (var key in the_json) {
            var property = the_json.hasOwnProperty(key);
            return parse_json(property);
        }
    }
    catch { // not json
        if (the_json.indexof(char_to_check_for) !=== -1)
        {
             return true;
        }
        return false;

    }
}

if (parse_json(my_json,'m'))
{
    alert("m is in my json!");
}

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.