0

I have a string that I get from a websocket in the below JSON format. I want a very low-latency way to parse the value c associated with the key C. The key names are the same for every packet I get, but the values may differ. So, the key C will stay the same but the value can change from c to something perhaps longer. In my real life application, the number of entries inside X and Y can be much longer.

I've tried a few different approaches, including parsing a single field using Jackson [1], to parsing the full string to JsonNode, but they're all too slow (over 50 microseconds). I thought of finding the position in the String of "C" using [2], and then taking the next few characters after that, but the issue is that the value, c, is variable length so that makes it tricky.

String s = {"A":"a","B":"b","data":[{"C":"c","X":[["3.79","28.07","1"],["3.791","130.05","3"],["3.792","370.8958","5"]],"Y":[["3.789","200","1"],["3.788","1238.1709","4"],["3.787","513.4051","3"]]}'

I'd like something like this:

String valueOfC = getValueOfC(s) // return in only a few microseconds

[1] How to read single JSON field with Jackson

[2] Java: method to get position of a match in a String?

JacksonCounty
  • 115
  • 2
  • 8
  • You can use `JsonPath`: [Iterate over a large JSON Array with JSONPath](https://stackoverflow.com/questions/55366515/iterate-over-a-large-json-array-with-jsonpath/55367535#55367535), [Parse only one field in a large JSON string](https://stackoverflow.com/questions/54852415/parse-only-one-field-in-a-large-json-string/54854433#54854433),[How to modify the value of a JsonNode recursively using Jackson](https://stackoverflow.com/questions/55560223/how-to-modify-the-value-of-a-jsonnode-recursively-using-jackson/55581639#55581639) – Michał Ziober Jan 23 '20 at 12:37
  • You could take a look at https://github.com/anatolygudkov/green-jelly (plain JSON finite state machine) This is one of the fastest way to parse JSON available in pure Java (without simd directly available). This is also GC free. But if you work with short Strings, standard methods like indexOf may work better since they are implemented as intrinsic. So, to make right choice, test your code with JMH – AnatolyG Jan 25 '20 at 13:25

1 Answers1

-2
s.substring(s.indexOf("\"data\":[{\"C") + 4, s.indexOf("\"",s.indexOf("\"data\":[{\"C") + 4)));

This is sub-microsecond.

JacksonCounty
  • 115
  • 2
  • 8