0

I have json file in below format.

{
    "data":[ 
        {
          "prjId": 1,
          "name" : "Forj1",
          "issue": [
                    {
                      "id": 00001,
                      "status" : "Closed"
                    },
                    {
                      "id": 00002,
                      "status" : "Open"
                    }
                ]
          },  
          {
          "prjId": 2,
          "name" : "Forj2",
          "issue": [
                    {
                      "id": 00003,
                      "status" : "Closed"
                    },
                    {
                      "id": 00004,
                      "status" : "Open"
                    }
                ]
          }],
    "issueCounter": 7,
    "success": true
}

Here "data" is array of projects, and within project attribute there is array of "issue".

So far if I remove "issue" array, I am able to traverse the json to one level down in "data" attribute, If this json has "issue" array I get an error saying missing comma.

javax.json.stream.JsonParsingException: Invalid token=NUMBER at (line no=15, column no=14, offset=242) Expected tokens are: [COMMA]

Below is the code that I have right now. I have two problems with this, one is the error while reading if I place the "issue" attribute, and secondly a way to read the "issue" array and traverse all attributes within.

InputStream fis = new FileInputStream(pathToFile+"data3.json");
JsonReader jsonReader = Json.createReader(fis);

//the error is thrown on below line while reading the above json. 

JsonObject jsonObject = jsonReader.readObject();

jsonReader.close();
fis.close();

System.out.println(jsonObject.getInt("issueCounter"));

//reading arrays from json
JsonArray jsonArrayData = jsonObject.getJsonArray("data");
Project [] prj = new Project[jsonArrayData.size()];
int index = 0;
for(JsonValue value : jsonArrayData){

    JSONObject jsonObj = new JSONObject(value.toString());
    System.out.println(jsonObj.getString("name"));
    System.out.println(jsonObj.getInt("prjId"));

    //this is also the place where I am stuck, I know I need to construct an array out of it by obtaining issue attribute. Below is very very wrong.
    /*
    JsonArray jsonArrayIssue = jsonObj.getJsonArray("issue");
    for(JsonValue issue : jsonArrayIssue){

        JSONObject jsonIssueObj = new JSONObject(issue.toString());
        System.out.println(jsonIssueObj.getString("status"));
        System.out.println(jsonIssueObj.getInt("id"));
    }
    */
}

Any help or pointers is deeply appreciated. I can tweak the json if its required ultimately I need to maintain an array of issues.

1
  • One alternative would be to just use gson and let use reflection for you Commented Dec 7, 2016 at 11:55

3 Answers 3

3

The problem as others said is the JSON. "id": 00001 <-- this is a number, numbers cannot start with a leading zero as per JSON stadard. If you control the JSON you should tweak it.

Alternatively ff you don't, you can use a less strict parser like org.json.simple https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple The code will be the same as yours, just adjusted to org.json.simple

try {   ...
        JSONObject rootJSON = (JSONObject) new JSONParser().parse(jsonString);
        JSONArray dataList = (JSONArray) rootJSON.get("data");
        for(Object projectObj: dataList.toArray()){
            JSONObject project = (JSONObject)projectObj;
            JSONArray issueList = (JSONArray) project.get("issue");
            for(Object issueObj: issueList.toArray()){
                JSONObject issue = (JSONObject) issueObj;
                //do something with the issue
            }
        }
    } catch (ParseException e) {
        //do smth
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help. Changing the number to string as mentioned by below post and then parsing it the way shown by you worked for me.
1

Your json data is invalid.You can check here. http://jsonlint.com
...issue": [{ "id": 00001,
"status": ----------------------^
Your id must be string number,string,boolean.Send 1,2,3,.... as return values and check if it works.

1 Comment

Thanks for the help, changing it to String helped along with the parsing process given by "isah".
1

Your code looks okay the problem is the JSON formatting. Specifically the following lines:

"id": 00001, "id": 00002, "id": 00003, "id": 00004,

Basically if you want it in that format you will need to set them as strings by wrapping the values in quotations i.e. "id": "00001" or you can use a valid number i.e. "id": 1

1 Comment

Thanks for the help, changing it to String helped along with the parsing process given by "isah".

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.