strybushnyi
strybushnyi

Reputation: 19

Parsing nested JSON in Java

I have a weird JSON which looks like this

[
  [
    {
      "id": "1",
      "clientId": "user"
    },
    {
      "id": "2",
      "clientId": "user"
    } 
 ],
  [
    {
      "Status": "NotCompleted",
      "StatusId": 0
    },
    {
      "Status": "Importing",
      "StatusId": 10
    }
  ]
]

I am trying to parse it with Gson or JsonParser.

Classes look like this

public class Event {
    public String id;
    public String clientId;
}

public class Status {
    public String Status;
    public String StatusId;
}

public class AllEvents {

public Event[] events;
public Status[] statuses;
}

But when I am trying to parse it with Gson (e.g)

  AllEvents[] r = new Gson().fromJson(response, AllEvents[].class);

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 6 path $[0]

Could you please help me out with parsing this kind of model? Cannot find what I am doing wrong in this case.

Thanks in advance

Upvotes: 1

Views: 115

Answers (3)

Ashish Karn
Ashish Karn

Reputation: 1143

To solve such a problem there is two approaches:

First Approach: Change your JSON file content(allEvents) to :

[
  {
    "events": [
      {
        "id": "1",
        "clientId": "user"
      },
      {
        "id": "2",
        "clientId": "user"
      }
    ],
    "statuses": [
      {
        "Status": "NotCompleted",
        "StatusId": 0
      },
      {
        "Status": "Importing",
        "StatusId": 10
      }
    ]
  }
]

and after that, your code will work perfectly.

Second Approach:

you need to code according to match above JSON structure:

Please find below the code which will help you.

    Gson gson = new Gson();
    Object[] r = gson.fromJson(loadDataAsString(), Object[].class);
    AllEvents allEvents = new AllEvents();
    //if your json structure position is fixed the do this commented code
    //allEvents.events = gson.fromJson(gson.toJson(r[0]), Event[].class); //if your json Event structure position is fixed at 0 index
    //allEvents.statuses = gson.fromJson(gson.toJson(r[1]), Status[].class); //if your json Status structure position is fixed at 1 index
    //if your json structure position is not fixed the do below code
    allEvents.events = Arrays.stream(r)
            .flatMap(x -> Arrays.stream(gson.fromJson(gson.toJson(x), Event[].class)))
            .filter(y -> y.id != null).toArray(Event[]::new);//id as primary key
    allEvents.statuses = Arrays.stream(r)
            .flatMap(x -> Arrays.stream(gson.fromJson(gson.toJson(x), Status[].class)))
            .filter(y -> y.Status != null).toArray(Status[]::new);//Status as primary key
    System.out.println(gson.toJson(allEvents));//{"events":[{"id":"1","clientId":"user"},{"id":"2","clientId":"user"}],"statuses":[{"Status":"NotCompleted","StatusId":"0.0"},{"Status":"Importing","StatusId":"10.0"}]}

Upvotes: 1

strybushnyi
strybushnyi

Reputation: 19

Solved issue in this way:

org.json.JSONArray allEvents = new org.json.JSONArray(response.getBodyAsString());

As a result I received JSONArray with 2 elements. Then extracted needed one through

allEvents.getJSONArray(0)

And then mapped in previous way with jackson ObjectMapper.

Thx for replies!

Upvotes: 0

armagedescu
armagedescu

Reputation: 2160

Use JsonReader instead. So if you know you json starts with [ and ends with ] use read with beginArray. Your in is an InputStream, it can be a file, socket stream or string stream.

     JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
     List<YourMessage> messages = new ArrayList<YourMessage>();

     reader.beginArray();
     while (reader.hasNext()) {
        messages.add (do something with reader);//<-- that is pseudo code
     }
     reader.endArray();
     return messages;

For more detail check this link https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.0/com/google/gson/stream/JsonReader.html

Upvotes: 0

Related Questions