1

I have a question about a json. Is the json below a standard of some kind. I can see why its preferable as the element names are written once and datatypes are included. I cannot change how the JSON comes in, but is there a Java library that easily parses it? Of course I can loop and loop but there must be something that parses it. I tried both with Jackson and org.json but both fail as it is an array at start. Also "jq" parses it and I can get to the value "100" that I want as the COUNT but if the schema comes in different than directly referencing the array indexes would break.

[
  {
    "header": {
      "queryId": "query_1590948930986",
      "schema": "`ROWKEY` STRING KEY, `USERID` STRING, `ENTITYTYPE` STRING, `ENTITYID` STRING, `DATESEARCHED` STRING, `COUNT` BIGINT"
    }
  },
  {
    "row": {
      "columns": [
        "6938bb62-50a1-4d0b-a113-1ecca1082763|+|n|+|09066437|+|2020-05-11",
        "6938bb62-50a1-4d0b-a113-1ecca1082763",
        "n",
        "09066437",
        "2020-05-11",
        100
      ]
    }
  }
]

in jq I can do this. but will fail if schema element comes in different.

| jq '.[1].row.columns[5]'
Robin Moffatt
  • 30,382
  • 3
  • 65
  • 92
user3008410
  • 644
  • 7
  • 15
  • I don't have an answer but I do have an issue for you to upvote/comment on :) https://github.com/confluentinc/ksql/issues/3526 – Robin Moffatt Jun 01 '20 at 12:37

1 Answers1

0

The returned JSON is valid. It is a JSON array containing two JSON Objects, each defining different properties.

In this specific case, the first element in the array contains metadata about the response, including the query id, (which may be included in server log lines or errors in the processing log), and the schema of the data returned in the row.columns array for the subsequent elements.

For example, the value "6938bb62-50a1-4d0b-a113-1ecca1082763|+|n|+|09066437|+|2020-05-11" corresponds to the ROWKEY STRING KEY column.

You can parse this data using Jackson. However, Jackson expects all elements in the array to share a common type. To deserialize using Jackson you will need a common base type, e.g. ResponseElement and two sub-types, e.g. HeaderElement and RowElement, with appropriate fields. You can then deserialize the JSON as a List<ResponseElement>.

More information on how to deserialize arrays with different element types can be found here:

How to deserialize a JSON array of elements with different types and varying number of elements?

Andrew Coates
  • 1,775
  • 1
  • 10
  • 16