-1

I have a json object I'm not able to deserialize. The json object comes from Alpaca Markets api/snapshots. I'm using Refit, but able to create a class set to read this, so I have read the json object directly, but are not able to reserialze this either using newtonsoft.json library. Here's the json object;

{
   "AAPL":{
      "latestTrade":{
         "t":"2021-09-17T20:25:51.020841773Z",
         "x":"V",
         "p":145.98,
         "s":100,
         "c":[
            "@",
            "T"
         ],
         "i":12505,
         "z":"C"
      },
      "latestQuote":{
         "t":"2021-09-17T20:00:43.70526836Z",
         "ax":"V",
         "ap":0,
         "as":0,
         "bx":"V",
         "bp":0,
         "bs":0,
         "c":[
            "R"
         ],
         "z":"C"
      },
      "minuteBar":{
         "t":"2021-09-17T20:25:00Z",
         "o":145.98,
         "h":145.98,
         "l":145.98,
         "c":145.98,
         "v":100,
         "n":1,
         "vw":145.98
      },
      "dailyBar":{
         "t":"2021-09-17T04:00:00Z",
         "o":148.75,
         "h":148.78,
         "l":145.765,
         "c":146.09,
         "v":1463230,
         "n":12487,
         "vw":146.742795
      },
      "prevDailyBar":{
         "t":"2021-09-16T04:00:00Z",
         "o":148.47,
         "h":148.95,
         "l":147.23,
         "c":148.78,
         "v":851556,
         "n":7523,
         "vw":148.174742
      }
   },
   "MSFT":{
      "latestTrade":{
         "t":"2021-09-17T20:00:58.520078539Z",
         "x":"V",
         "p":300.16,
         "s":100,
         "c":[
            "@",
            "T"
         ],
         "i":9410,
         "z":"C"
      },
      "latestQuote":{
         "t":"2021-09-17T20:00:00.000042133Z",
         "ax":"V",
         "ap":0,
         "as":0,
         "bx":"V",
         "bp":0,
         "bs":0,
         "c":[
            "R"
         ],
         "z":"C"
      },
      "minuteBar":{
         "t":"2021-09-17T20:00:00Z",
         "o":300.16,
         "h":300.16,
         "l":300.16,
         "c":300.16,
         "v":100,
         "n":1,
         "vw":300.16
      },
      "dailyBar":{
         "t":"2021-09-17T04:00:00Z",
         "o":304.245,
         "h":304.37,
         "l":299.59,
         "c":299.87,
         "v":570598,
         "n":9412,
         "vw":300.820404
      },
      "prevDailyBar":{
         "t":"2021-09-16T04:00:00Z",
         "o":303.84,
         "h":305.31,
         "l":300.82,
         "c":305.24,
         "v":386250,
         "n":6166,
         "vw":303.325332
      }
   }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • What exactly is your problem? What have you tried, that does not work? Looks like you could deserialize this to a `Dictionary` where `Snapshot` is a POCO created to deserialize the data for one ticker. – dbc Sep 19 '21 at 23:43
  • What have you tried? What makes you think it doesn't work. It's a bit hard for us to guess – Flydog57 Sep 19 '21 at 23:43
  • 1
    @dbc - I tried deserialize this to a Dictionary and it's working. Thanks for your input. – Dag Rasen Sep 20 '21 at 01:47
  • @DagRasen - you're welcome. I found a couple of similar questions with answers that say to use a `Dictionary` in such situations, so I went ahead and marked this as a duplicate. – dbc Sep 20 '21 at 05:36

1 Answers1

0

try this, it was tested and working properly (using newtonsoft.json library)

var json=...your json string;

var jD = JsonConvert.DeserializeObject<Dictionary<string,Aapl>>(json);

json=JsonConvert.SerializeObject(jD);

output

{"AAPL":{"latestTrade":{"t":"2021-09-17T20:25:51.0208418+00:00","x":"V","p":145.98,"s":100,"c":["@","T"],"i":12505,"z":"C"},"latestQuote":{"t":"2021-09-17T20:00:43.7052684+00:00","ax":"V","ap":0,"as":0,"bx":"V","bp":0,"bs":0,"c":["R"],"z":"C"},"minuteBar":{"t":"2021-09-17T20:25:00+00:00","o":145.98,"h":145.98,"l":145.98,"c":145.98,"v":100,"n":1,"vw":145.98},"dailyBar":{"t":"2021-09-17T04:00:00+00:00","o":148.75,"h":148.78,"l":145.765,"c":146.09,"v":1463230,"n":12487,"vw":146.742795},"prevDailyBar":{"t":"2021-09-16T04:00:00+00:00","o":148.47,"h":148.95,"l":147.23,"c":148.78,"v":851556,"n":7523,"vw":148.174742}},"MSFT":{"latestTrade":{"t":"2021-09-17T20:00:58.5200785+00:00","x":"V","p":300.16,"s":100,"c":["@","T"],"i":9410,"z":"C"},"latestQuote":{"t":"2021-09-17T20:00:00.0000421+00:00","ax":"V","ap":0,"as":0,"bx":"V","bp":0,"bs":0,"c":["R"],"z":"C"},"minuteBar":{"t":"2021-09-17T20:00:00+00:00","o":300.16,"h":300.16,"l":300.16,"c":300.16,"v":100,"n":1,"vw":300.16},"dailyBar":{"t":"2021-09-17T04:00:00+00:00","o":304.245,"h":304.37,"l":299.59,"c":299.87,"v":570598,"n":9412,"vw":300.820404},"prevDailyBar":{"t":"2021-09-16T04:00:00+00:00","o":303.84,"h":305.31,"l":300.82,"c":305.24,"v":386250,"n":6166,"vw":303.325332}}}

classes

    public partial class Aapl
    {
        [JsonProperty("latestTrade")]
        public LatestTrade LatestTrade { get; set; }

        [JsonProperty("latestQuote")]
        public LatestQuote LatestQuote { get; set; }

        [JsonProperty("minuteBar")]
        public Bar MinuteBar { get; set; }

        [JsonProperty("dailyBar")]
        public Bar DailyBar { get; set; }

        [JsonProperty("prevDailyBar")]
        public Bar PrevDailyBar { get; set; }
    }

    public partial class Bar
    {
        [JsonProperty("t")]
        public DateTimeOffset T { get; set; }

        [JsonProperty("o")]
        public double O { get; set; }

        [JsonProperty("h")]
        public double H { get; set; }

        [JsonProperty("l")]
        public double L { get; set; }

        [JsonProperty("c")]
        public double C { get; set; }

        [JsonProperty("v")]
        public long V { get; set; }

        [JsonProperty("n")]
        public long N { get; set; }

        [JsonProperty("vw")]
        public double Vw { get; set; }
    }

    public partial class LatestQuote
    {
        [JsonProperty("t")]
        public DateTimeOffset T { get; set; }

        [JsonProperty("ax")]
        public string Ax { get; set; }

        [JsonProperty("ap")]
        public long Ap { get; set; }

        [JsonProperty("as")]
        public long As { get; set; }

        [JsonProperty("bx")]
        public string Bx { get; set; }

        [JsonProperty("bp")]
        public long Bp { get; set; }

        [JsonProperty("bs")]
        public long Bs { get; set; }

        [JsonProperty("c")]
        public string[] C { get; set; }

        [JsonProperty("z")]
        public string Z { get; set; }
    }

    public partial class LatestTrade
    {
        [JsonProperty("t")]
        public DateTimeOffset T { get; set; }

        [JsonProperty("x")]
        public string X { get; set; }

        [JsonProperty("p")]
        public double P { get; set; }

        [JsonProperty("s")]
        public long S { get; set; }

        [JsonProperty("c")]
        public string[] C { get; set; }

        [JsonProperty("i")]
        public long I { get; set; }

        [JsonProperty("z")]
        public string Z { get; set; }
    }
Serge
  • 40,935
  • 4
  • 18
  • 45
  • 1
    You cannot know what symbols you'll have there beforehand. Just deserialize to `Dictionary`, where `Snapshot` is the class currently named `Aapl`. – Jimi Sep 19 '21 at 23:58
  • @Jimi Thank you. I am going to wait for a PO feedback. I can't see anything about symbols there. – Serge Sep 19 '21 at 23:59
  • 1
    @serge - the problem is dynamic symbols like AAPL & MSFT – Dag Rasen Sep 20 '21 at 01:39
  • @DagRasen You should put it in your question. I ajasted my answer accordingly. – Serge Sep 20 '21 at 06:17