0

I have a json output as

amt: "10.00"
email: "[email protected]"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"

Literally Im trying to do this in javascript

if(the json output has the key merchant_id)
{
//do something
}

How is it possible to find if a json output has a key like as I demonstrated above?Is it possible,Or is there any alternate method for it?

5
  • possible duplicate of Checking if an associative array key exists in Javascript Commented Dec 27, 2013 at 16:06
  • Is this a multiline string? Or is it a json object? What's the exact format and type of the variable you get your data in? Commented Dec 27, 2013 at 16:08
  • Yep, then it's the easy way, if(x.merchant_id=="whatYouWant") Commented Dec 27, 2013 at 16:12
  • hey no yaar.I want to search if 'merchant_id' exists,not its value.If(json has merchant_id){// } like this Commented Dec 27, 2013 at 16:13
  • stackoverflow.com/a/1098955/1636522, not the answer you are looking for? Commented Jan 1, 2014 at 6:57

4 Answers 4

1

Let's say you have JSON object like this:

var json = {
   amt: "10.00",
   email: "[email protected]",
   merchant_id: "sam",
   mobileNo: "9874563210",
   orderID: "123456",
   passkey: "1234"
}

So now you have to use a loop. E.g.

for(var j in json) { // var j is the key in this loop
   if(j == 'merchant_id') {
      // do something
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can just check the type of of the field.

if (typeof json.property !== 'undefined') {
    //has property
}

2 Comments

Can you specify this with my question,so it wil be more clear
If you have a JSON formatted like this: var json = { amt: "10.00", email: "[email protected]", merchant_id: "sam", mobileNo: "9874563210", orderID: "123456", passkey: "1234" } and you need to check the existance of the orderID field just do: if (typeof json.orderID!== 'undefined') { //has property }
0

If this is a multiline string like you wrote it, a simple way is:

if(s.indexOf("\nmerchant_id: \"whatever\"\n")>-1) {
    // ...
}

But you can parse it of course, and that's the elegant way. With a json object, you can just refer to the field with a dot, like "obj.merchant_id".

5 Comments

Its a json object actually which is parsed
Easy pie then. Use the dot syntax mentioned above. (Also if you're hunting for the key only, not regarding its value, franchetto is right.)
so how it wil come,like this? I dont want to check the value of the key,but i want to search if this key exists in the object
But I didnt understand his answer any more,can you help?
Check franchetto's answer
0

For example, try this:

var data = {
   amt: "10.00",
   email: "[email protected]",
   merchant_id: "sam",
   mobileNo: "9874563210",
   orderID: "123456",
   passkey: "1234"
}

if(data.amt == 'something'){
   // do work
}

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.