0

I'm trying to check if a string is in a JSON object with javascript. I don't know if it is possible or I have to convert something. Here is the part of the code with the if statement in which I want to check if data.userName (the string) is in users (the JSON object)

function validation() {
    var userName_login = document.getElementById("username").value;
    var password_login = document.getElementById("password").value;
    var data = {
        userName: userName_login,
        password: password_login
    };
    doJSONRequest("GET", "/users/", null, data, function(users) {
        if (data.userName) {

        }
    })
}

And the doJSONRequest function is:

function doJSONRequest(method, url, headers, data, callback) {
    if (arguments.length != 5) {
        throw new Error('Illegal argument count');
    }
    doRequestChecks(method, true, data);
    var r = new XMLHttpRequest();
    r.open(method, url, true);
    doRequestSetHeaders(r, method, headers);
    r.onreadystatechange = function() {
        if (r.readyState != 4 || (r.status != 200 && r.status != 201 && r.status != 204)) {
            return;
        } else {
            if (isJSON(r.responseText))
                callback(JSON.parse(r.responseText));
            else
                callback();
        }
    };
    var dataToSend = null;
    if (!("undefined" == typeof data) && !(data === null))
        dataToSend = JSON.stringify(data);
    r.send(dataToSend);
}
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
pp94
  • 133
  • 5
  • 19
  • http://stackoverflow.com/questions/18363618/check-if-json-string-has-value-in-js – TheSuper Nov 28 '14 at 18:51
  • if it's not json, then json.parse will throw an error. do a try/catch. if you catch, then it's not json. – Marc B Nov 28 '14 at 18:52
  • possible duplicate of [How to test if a string is JSON or not?](http://stackoverflow.com/questions/9804777/how-to-test-if-a-string-is-json-or-not) – Zakaria Nov 28 '14 at 18:53
  • Ehm.... No. I don't want to know if a string IS a JSON or not but if a string IS IN a JSON... – pp94 Nov 28 '14 at 19:42
  • @SH.TheSuper you are right. Sorry for the duplicate. I put the question in the wrong way and I didn't notice that it was already there – pp94 Nov 28 '14 at 20:02

2 Answers2

3
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;
}

check if Json string has value in JS

Community
  • 1
  • 1
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • you are right. Sorry for the duplicate. I put the question in the wrong way and I didn't notice that it was already there – pp94 Nov 28 '14 at 20:02
2

Just try to parse it using JSON.parse, if the parse was successful return true else return false:

function isJSON(str) {
    try { 
      JSON.parse(str);
    } catch (e) {
      return false;
    }
    return true;
}
ianaya89
  • 4,153
  • 3
  • 26
  • 34