How to parse JSON quickly and simply

The runners field of jsonstr.json is a sub document. The sub document has three fields: horseid, ownercolors, trainer, among which trainer contains the lower level field trainerid. We need to query the horseid, ownercolors, trainerid fields of the corresponding sub document according to the document sequence number. Some of the source data are as follows:

[

   {

      "race": {

          "raceId":"1.33.1141109.2",

          "meetingId":"1.33.1141109"

      },

      ...

      "numberOfRunners": 2,

      "runners": [

        {

              "horseId":"1.00387464",

              "trainer": {

                  "trainerId":"1.00034060"

              },

          "ownerColours":"Maroon,pink sleeves,dark blue cap."

          },

          {

              "horseId":"1.00373620",

              "trainer": {

                  "trainerId":"1.00010997"

              },

          "ownerColours":"Black,emerald green cross of lorraine,striped sleeves."

          }

      ]

   },

...

]

Expect the structured effect of JSON:

Java only provides the basic class library to parse JSON, but there are many and complicated API interfaces. If you want to do in-depth calculation, you still need complex hard coding. It should be written as follows:

...

JSONObject jsonObject = JSONObject.fromObject(s);

JSONArray result = jsonObject.getJSONArray("runners");

for (int i = 0; i < result.size(); i++) {

        JSONArray index = result.getJSONObject(i).getJSONArray("index");

        ...

}

...

It is much simpler to use esProc SPL for JSON parsing. It encapsulates the JSON class library and has rich and powerful set operation ability, so it is easy to deal with JSON parsing. For example, in the above problem, take out the first runners field (sub document), which only needs 3 lines:

 

A

1

=json(file("/workspace/JSONstr.json").read())

2

=A1(1).runners

3

=A2.new(horseId,trainer.trainerId:trainerId,ownerColours)

In fact, there are many cases where it is not convenient to parse JSON in Java, and other requirements such as calculation and importing into database, but it is very simple to use esProc SPL. For your interest, please refer to: JSON data calculation and importing into database

It's also easy to embed esProc into Java applicationsplease refer to How to Call an SPL Script in Java

For the installation, usage, free authorization and related technical information of esProc, please refer to Getting started with esProc