0

I have a retrofit request that returns the JSON object below. The JSON is a list of objects starting with stock ticker name which is dynamic (could be AAPL/GOOG/MSFT...etc).

How can i create a kotlin data class to de-serialize this JSON?

Note i do not want to hardcode AAPL/GOOG etc but rather have a dynamic data class that can be used based on the ticker name and whether pulling 3,4,5 tickers or more. I would also like to avoid using Gson or other third party libraries.

{
  "AAPL": {
    "meta": {
      "symbol": "AAPL",
      "interval": "1min",
      "currency": "USD",
      "exchange_timezone": "America/New_York",
      "exchange": "NASDAQ",
      "type": "Common Stock"
    },
    "values": [
      {
        "datetime": "2021-12-31 15:59:00",
        "open": "177.61000",
        "high": "177.75000",
        "low": "177.55000",
        "close": "177.59000",
        "volume": "1536914"
      },
      {
        "datetime": "2021-12-31 15:58:00",
        "open": "177.56000",
        "high": "177.67999",
        "low": "177.49001",
        "close": "177.61000",
        "volume": "650356"
      }
    ],
    "status": "ok"
  },
  "GOOG": {
    "meta": {
      "symbol": "GOOG",
      "interval": "1min",
      "currency": "USD",
      "exchange_timezone": "America/New_York",
      "exchange": "NASDAQ",
      "type": "Common Stock"
    },
    "values": [
      {
        "datetime": "2021-12-31 15:59:00",
        "open": "2895.86011",
        "high": "2897.14990",
        "low": "2893.55005",
        "close": "2893.58008",
        "volume": "47979"
      },
      {
        "datetime": "2021-12-31 15:58:00",
        "open": "2895.70996",
        "high": "2896.87012",
        "low": "2895.40991",
        "close": "2896.36255",
        "volume": "21948"
      }
    ],
    "status": "ok"
  },
  "MSFT": {
    "meta": {
      "symbol": "MSFT",
      "interval": "1min",
      "currency": "USD",
      "exchange_timezone": "America/New_York",
      "exchange": "NASDAQ",
      "type": "Common Stock"
    },
    "values": [
      {
        "datetime": "2021-12-31 15:59:00",
        "open": "336.39001",
        "high": "336.54001",
        "low": "336.10999",
        "close": "336.20999",
        "volume": "472414"
      },
      {
        "datetime": "2021-12-31 15:58:00",
        "open": "336.59000",
        "high": "336.67999",
        "low": "336.39001",
        "close": "336.39001",
        "volume": "155067"
      }
    ],
    "status": "ok"
  }
}

Blockquote

data class Ticker (

    /*????How to handle list of objects with dynamic ticker names here????*/
    var Meta: Meta,
    var values: List<Values>,
    var status: String

)

data class Meta(

    var symbol: String,
    var interval: String,
    var currency: String,
    var currencyQuote: String,
    var exchange: String,
    var exchangeTimezone: String,
    var type: String,

    )

data class Values (

    var datetime: String,
    var open: String,
    var high: String,
    var low: String,
    var close: String,
    var volume: String

)

1 Answer 1

1

The payload you have provided is not a list, it's a Map<String, Ticker>.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Mafor, are you saying that the native de-serialized version of this JSON is a Map?
I'm not sure what you mean by the "native deserialization version". Technically, the JSON you've provided is a json object, and as such, it can be deserialized either to a concrete class, with hardcoded field names, or to a map, but not to a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.