Hector
Hector

Reputation: 5344

How to extract last item in Json Array without parsing the entire Json message

My current Android application is employing RealmIO to store Json data.

I use the following Realm insert

backgroundRealm.createOrUpdateAllFromJson(Data.class, rawJson);

My raw Json data is retrieved via a REST API

The API supports paging however I need to pass subsequent calls the last Id contained within the previous rawJson.

These Json arrays have 25000 items. I dont want the cost of parsing the entire 25000 item array.

Is there any method I can use to extract the last array item to discover what the last Id value is?

each item resembles this

{"id":6,"risk":"xxxxxxxxx","active":0,"from":"2016-07-18"}

What options do I have?

is it just rawJson.lastIndexOf("id") and substring()?

Upvotes: 0

Views: 273

Answers (1)

9000
9000

Reputation: 40884

There's no standard way to avoid parsing the entire JSON. You can, on the other hand, avoid storing it all in RAM, and extracting it into nodes. Take a streaming JSON parser (these exist) and watch for the events of the element you need. The last event of this kind you receive will contain the last ID.

On the other hand, if you know that the schema and the serialization format of the JSON are not going to change (e.g. you control it), you can just scan the text of unparsed JSON from the end and extract that value, by counting parens and quotes. This is, of course, a brittle approach, though very fast. Its brittleness definitely depends on the schema: if the list you're looking for is a last element of another list, it's much more robust than if it's a value under a particular key, and can end up anywhere in the map.

If you are in control of the REST API side of the app, consider rethinking it, e.g. passing the events from latest to earliest, so you can look for the first events while parsing, and safely discard the rest if you don't need the past.

Upvotes: 1

Related Questions