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);
}
5
  • stackoverflow.com/questions/18363618/… Commented Nov 28, 2014 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. Commented Nov 28, 2014 at 18:52
  • possible duplicate of How to test if a string is JSON or not? Commented Nov 28, 2014 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... Commented Nov 28, 2014 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 Commented Nov 28, 2014 at 20:02

2 Answers 2

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

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

1 Comment

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

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.