find key exist in json onject

check if key value exists in json object javascript
check if key exists in json object c#
check if property exists in json object c#
vba check if json key exists
find a particular key in json object
how to get key and value from json array object in javascript
javascript check if json object
jsonpath check if key exists
"questions": [{
        "_id": "5b2bc4f6f1dacd2b0ca65bca",
        "updatedAt": "2018-06-21T15:32:06.237Z",
        "createdAt": "2018-06-21T15:32:06.237Z",
        "title": "What is Full form of RC",
        "answer": "opt4",
        "testId": "5b2bbccd8b30aa2a64327645",
        "__v": 0,
        "opt4": "Registeraction Certificate",
        "opt3": "Requirement Certificate",
        "opt2": "Registeration Common",
        "opt1": "Registeration Counter",
        "solution": "RC means Registeraction Certificate"
    },
    {
        "_id": "5b2bc4faf1dacd2b0ca65bcd",
        "updatedAt": "2018-06-21T15:32:10.566Z",
        "createdAt": "2018-06-21T15:32:10.566Z",
        "title": "What is Full form of RC",
        "answer": "opt4",
        "testId": "5b2bbccd8b30aa2a64327645",
        "__v": 0,
        "opt4": "Registeraction Certificate",
        "opt3": "Requirement Certificate",
        "opt2": "Registeration Common",
        "opt1": "Registeration Counter",
        "solution": "RC means Registeraction Certificate"
    }
]

Above are my data which using in ques.map()

var ans = {
    "5b2bc4f6f1dacd2b0ca65bca": "opt4",
    "5b2bc4faf1dacd2b0ca65bcd": "opt2",
    "5b2bc4fcf1dacd2b0ca65bcf": "opt2",
    "5b2bc4fff1dacd2b0ca65bd1": "opt4",
    "5b2bc501f1dacd2b0ca65bd3": "opt2",
    "5b2bc504f1dacd2b0ca65bd6": "opt2",
    "5b2bc5facdf99b18a0b9722b": "opt2",
    "5b2bc6295865b528dceeb6b7": "opt2",
    "5b2bc6ba7f32b21f7c68e57f": "opt2",
    "5b2bc6bc7f32b21f7c68e580": "opt2"
}

I need to find if key exists in above json object, but I'm not able to get it. Its always returns false.

console.log(ans);
ques.map(function(item) {
    var thisIs = ans.hasOwnProperty(item._id)
    console.log(thisIs);
})

It always return false

You can use match the key in the object using for loop.

ques.forEach((item)=>{
  for (var key in ans) {
    if (key == item._id){
      return true;
    }
  }
});

How to check if a key exists in Json Object and get its value, Given a JSON Object, the task is to check whether a key exists in Object or not using JavaScript. We're going to discuss few methods. hasOwnProperty() thisSession["merchant_id"] will return false if the key does not exist, or if its value evaluates to false for any reason (e.g. if it's a literal false or the integer 0 and so on). share | improve this answer | follow | | | |

try this updated code

without jquery (down voter)

use

Object.keys(ques).map(function(key, index) {
  Object.keys(ques[key]).map(function(key1, index1) {
    var thisis = ans.hasOwnProperty(ques[key][key1]['_id']);
    console.log(thisis);
  });
 });

var ques = {
  "questions": [{
      "_id": "5b2bc4f6f1dacd2b0ca65bca",
      "updatedAt": "2018-06-21T15:32:06.237Z",
      "createdAt": "2018-06-21T15:32:06.237Z",
      "title": "What is Full form of RC",
      "answer": "opt4",
      "testId": "5b2bbccd8b30aa2a64327645",
      "__v": 0,
      "opt4": "Registeraction Certificate",
      "opt3": "Requirement Certificate",
      "opt2": "Registeration Common",
      "opt1": "Registeration Counter",
      "solution": "RC means Registeraction Certificate"
    },
    {
      "_id": "5b2bc4faf1dacd2b0ca65bcd",
      "updatedAt": "2018-06-21T15:32:10.566Z",
      "createdAt": "2018-06-21T15:32:10.566Z",
      "title": "What is Full form of RC",
      "answer": "opt4",
      "testId": "5b2bbccd8b30aa2a64327645",
      "__v": 0,
      "opt4": "Registeraction Certificate",
      "opt3": "Requirement Certificate",
      "opt2": "Registeration Common",
      "opt1": "Registeration Counter",
      "solution": "RC means Registeraction Certificate"
    }
  ]
}


var ans = {
  "5b2bc4f6f1dacd2b0ca65bca": "opt4",
  "5b2bc4faf1dacd2b0ca65bcd": "opt2",
  "5b2bc4fcf1dacd2b0ca65bcf": "opt2",
  "5b2bc4fff1dacd2b0ca65bd1": "opt4",
  "5b2bc501f1dacd2b0ca65bd3": "opt2",
  "5b2bc504f1dacd2b0ca65bd6": "opt2",
  "5b2bc5facdf99b18a0b9722b": "opt2",
  "5b2bc6295865b528dceeb6b7": "opt2",
  "5b2bc6ba7f32b21f7c68e57f": "opt2",
  "5b2bc6bc7f32b21f7c68e580": "opt2"
}


//console.log(ques);
Object.keys(ques).map(function(key, index) {
  Object.keys(ques[key]).map(function(key1, index1) {
    var thisis = ans.hasOwnProperty(ques[key][key1]['_id']);
    console.log(thisis);
  });

});

JavaScript, When you deal with json object you may want to check whether key exists in the jsonobject before fetching value for that key Trying to get a property off of an object that is not defined will raise an exception. You need to check each property for existence (and type) throughout the chain (unless you are sure of the structure).

Using @dr-strange answer as a basis for this answer Without Using map or anything related.

This is the basic code

Object.keys(ques).forEach(function(key) { //loop over upper level keys
  Object.keys(ques[key]).forEach(function(innerKey) { //loop second level keys
    var thisIs = ans.hasOwnProperty(ques[key][innerKey]['_id']); //compare keys
    console.log(thisIs);
    return; // break loop
  });

});

var ques = {
  "questions": [{
      "_id": "5b2bc4f6f1dacd2b0ca65bca",
      "updatedAt": "2018-06-21T15:32:06.237Z",
      "createdAt": "2018-06-21T15:32:06.237Z",
      "title": "What is Full form of RC",
      "answer": "opt4",
      "testId": "5b2bbccd8b30aa2a64327645",
      "__v": 0,
      "opt4": "Registeraction Certificate",
      "opt3": "Requirement Certificate",
      "opt2": "Registeration Common",
      "opt1": "Registeration Counter",
      "solution": "RC means Registeraction Certificate"
    },
    {
      "_id": "5b2bc4faf1dacd2b0ca65bcd",
      "updatedAt": "2018-06-21T15:32:10.566Z",
      "createdAt": "2018-06-21T15:32:10.566Z",
      "title": "What is Full form of RC",
      "answer": "opt4",
      "testId": "5b2bbccd8b30aa2a64327645",
      "__v": 0,
      "opt4": "Registeraction Certificate",
      "opt3": "Requirement Certificate",
      "opt2": "Registeration Common",
      "opt1": "Registeration Counter",
      "solution": "RC means Registeraction Certificate"
    }
  ]
}


var ans = {
  "5b2bc4f6f1dacd2b0ca65bca": "opt4",
  "5b2bc4faf1dacd2b0ca65bcd": "opt2",
  "5b2bc4fcf1dacd2b0ca65bcf": "opt2",
  "5b2bc4fff1dacd2b0ca65bd1": "opt4",
  "5b2bc501f1dacd2b0ca65bd3": "opt2",
  "5b2bc504f1dacd2b0ca65bd6": "opt2",
  "5b2bc5facdf99b18a0b9722b": "opt2",
  "5b2bc6295865b528dceeb6b7": "opt2",
  "5b2bc6ba7f32b21f7c68e57f": "opt2",
  "5b2bc6bc7f32b21f7c68e580": "opt2"
}



Object.keys(ques).forEach(function(key) {
  Object.keys(ques[key]).forEach(function(innerKey) {
    var thisIs = ans.hasOwnProperty(ques[key][innerKey]['_id']);
    console.log(thisIs);
    return;
  });
   });

Check if key exists in jsonobject before fetching value for that key ? -, we can easily check jquery json object key exists or not using hasOwnProperty. jquery json provide several method for getting key value or check  Check if the key exists or not in JSON. Let’s assume you received the following student, JSON. And you wanted to check if the percentage key is present or not in JSON data. if it is present directly to access its value instead of iterating the entire JSON.

How to check if key exists in json object in jquery?, The result is a JSON Object data type (Newtonsoft one). How do I check if some key exists in the JSON? Something like config.hasKey(… JSON should be with a String key and a value of whatever you need. – Nico Jul 24 '17 at 11:11 @Eido95, I believe for an integer there is always a value, every time you declare an integer, you have to initialize it with a value. – mut tony Feb 9 '18 at 18:54

How to Check if A JSON Object Contains A Certain Key?, bool containsKey(const char* key) const; bool containsKey(const String& key) const; bool containsKey(const std::string& key) const; bool true if the key is present is the JsonObject ; false if not. Instead, just get the value and test if it's NULL :. This keyExists(key, search) can be used to easily lookup a key within objects or arrays! Just pass it the key you want to find, and search obj (the object or array) you want to find it in.

JsonObject::containsKey(), get the value from JSON for given key if exists * @param json * @param key * @​return */ protected String getOrNull(JSONObject json, String key) { if (json != null  JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma. Values in a JSON object can be another JSON object.

Comments
  • Do you want questions for which _id is exist in ans?
  • You’re not returning anything from map() - use forEach() instead. Also your for loop can be replaced by another forEach() so you keep the code concise.
  • map is not a looping function`
  • Thanks, updated! I initially focused on matching the key. I didn't noticed that.
  • @Prachi nope. its returning false
  • It doesn't even enter the if statement?
  • surely there's a solution that doesn't require jQuery
  • but ques.map not a function for loop
  • what about for forEach while?
  • hello MR @Itamar plsss check my code without jquery
  • map is not needed, nor its a loop function