not able to parse JSON file,parsing a json

jsonfile.jpg as attached in the image, i am able to parse the JSON and get the "result" value after that i have a data array where i need to get the value of all corresponding "CASE_NUMBER" values.which am not able to do.

Can any one help me in getting the "CASE_NUMBER" value from the json file.

,

jsonfile.jpgI am facing problem in getting the parsed result.

as displayed in the image, i am able to parse and get the "result" value and after that i need CASE_NUMBER value for all the 59 cases.

Can anyone help me out in parsing and getting all "CASE_NUMBER" values from the json file.

0 3 3,385
3 REPLIES 3

Hmm, yes it seems the problem is that what you are trying to parse is encapsulated within a string.

I think what you expect is a JSON like this:

{
  "result" : [
    59,
    [ { 
        "CASE_NUMBER" : "xyz", 
        "Case_TITLE" : "Someone vs Someone else", 
         ...
       },
       { 
        "CASE_NUMBER" : "abc", 
        "Case_TITLE" : "Party1 vs Party2", 
         ...
       }
  ] 
}

But that is not what you have. The array of cases is encoded as a string. So the actual json is like this:

{
  "result" : [
    59,
    "a very long string here"
  ]
}

This is valid json. The result field is an array with exactly 2 elements: a number and a very long string. I think you are trying to treat the "very long string" as ... a JSON object, but it's not. It's a string.

To get what you want you must parse that string as JSON. The code might be like this:

var s = context.getVariable('response.content'); 
s = JSON.parse(s);
var ar = s.result[1];
ar = JSON.parse(ar); // parse the string as json! 
ar.forEach(function(item) {
  print(item.CASE_NUMBER);
  // do something with the case here
});

thanks for the response. but when i am trying to parse the last string it says "cannot parse a string which does not start with '{' "

Attached is the error pic and the data i tried to parseerror1.jpgcurrent-data.jpg

You didn't show me your code, but... inferring from the stacktrace, I think the library you are using is expecting a string that represents a JSON OBJECT, and your string represents a JSON Array. Maybe there is a different parse method or function in JRuby that you must use to parse a JSON Array.

I think it might be something like this:

JSONArray jsonArray = new JSONArray(yourStringHere);

If you try to parse the string in JavaScript, it will work nicely. JSON.parse() in JavaScript can handle a string that represents either an Array or an Object (aka hash).