1

i have this json returned from my ajax call

 [
{message:"haha", type:"error"}, 
{message:"nice work", type:"success"},
{message:"closed.", type:"success"}
  ]

and I need to find out if any of the items are of the type error. Now I know I can loop through and figure it out but I wonder if if there is a function that I could tell me what i need to know

3
  • Do you only need to know if there's an error anywhere, or do you need to know which specific one? Commented Feb 23, 2011 at 18:38
  • Just so you know, that's not valid JSON. You need to enclose each "Key"(?) with quotations. So {message: "haha", type: "error"} would become {"message": "haha", "type": "error"} Commented Feb 23, 2011 at 18:43
  • 1
    Then before you parse the JSON, you could just do an .indexOf(). I'll add an answer. Commented Feb 23, 2011 at 18:48

3 Answers 3

3

You'll have to loop. You can do it with a straightforward loop, or use jQuery.each if you like.

Off-topic: Note that what you've quoted isn't JSON. It's object literal notation. To be valid JSON, the key names would need to be in double quotes:

[
    {"message":"haha", "type":"error"}, 
    {"message":"nice work", "type":"success"},
    {"message":"closed.", "type":"success"}
]

Some "JSON" parsers (particularly those that are really JavaScript parsers in disguise) are lax and let you get away with it, but that's becoming less common.

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

13 Comments

I wouldn't call it off topic to point out the JSON syntax error. in fact, I'd give you a +1.
I'd guess that OP is posting the data post-parsing. Technically your corrected code is still not actual JSON.
@patrick: No, you're confusing strings with markup. What's presented is JSON markup. If you put it in program source code, then you'd have to put it in quotation marks. But any other use of it (reading it from a file, retrieving it via XHR, etc., etc.) you don't. Also, any whitespace (not just sapces) is valid between tokens. JSON is just a markup syntax, like HTML. You don't require that everyone put HTML in quotes, do you? :-)
@patrick: I'll say no more. We'll just have to agree to disagree. You think I'm making things more confusing, I think you are. Done.
I'll respect that. Thank you for the interesting conversation anyway. :o)
|
2

Prior to parsing the JSON, you could test the string for a match.

var json = '[{"message":"haha","type":"error"},{"message":"nice work","type":"success"},{"message":"closed.","type":"success"}]'

if( json.indexOf('"type":"error"') > -1 ) {
    // there was an error somewhere
}

If the json is a little loose with spaces around the keys/values, you could use a regular expression to test instead.

2 Comments

Our conversation on my answer totally aside: This seems brittle. As you mentioned with the possibility of spaces, .indexOf would probably cause confusion down the line when an errant space throws off the condition. Rather than resorting to regex, why not just parse the JSON and then loop it? After analyzing the structure of the JSON data, message probably contains the error message anyway, so the data will need to be parsed anyway.
@Stephen: Yes, I'd agree with all points. The only reason I posted this as a possible solution was that OP noted "Now I know I can loop through and figure it out but...". Searching the string was the only solution I could think of that didn't involve an explicit loop. I assume there's a bit of processing to do on the data, and OP didn't want a separate loop just to look for an error.
2
var json = [
    { "message" : "haha",      "type" : "error"  }, 
    { "message" : "nice work", "type" : "success"},
    { "message" : "closed.",   "type" : "success"}
];

$.each(json, function(i, k) {
    if (k.type === "error") {
        // this is an error
        // `k.message` contains the expected message
        // `i` contains the index key of the array, in this case `0` 
    }
});

Note: Although my example uses Array and Object literals to express the point (instead of actual JSON text), you should ensure that valid JSON keys are wrapped in quotation marks ¹.

1 for @patrick. :)

8 Comments

That still isn't JSON data. It's just Array and Object literals.
yeah... included for clarity, captain pedant.
Correcting legitimate errors or points of confusion is not pedantry. You stated that valid JSON keys must be wrapped in quotation marks. Then you proceeded to wrap the keys as such, and reference the data in a variable called json. Either you erroneously believed that was JSON data, or you've added to the confusion of others.
@patrick: If you really want to get technical, JSON is a text format. That means your accepted answer is also not JSON, it is a JavaScript string (not text... a scalar value. Technically, a string literal.) stored in a variable. Is it closer to correct? I'll give you that. But close only counts in horseshoes.
@patrick we have reached an impasse; You are the obvious JSON police, and I'm the renegade.
|

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.