0

Using the below JSON example and Newtsoft, I am trying to figure out what the class definitions should be to deserialize it. The memo is an array of braums. But braums has different key/values. I have this example https://dotnetfiddle.net/cIU3jS with classes. If I deserialize with memo as an array of string, then deserialize the string with the correct class it works as in the code example TestMemo(). If I try to let the array be of the class it does not. Do I have to do this in a two-step process? What is the best way, create a function that returns the correct class? Is there a way to have a "post" process to the deserialization that I can do the memo after it been split out?

{
  "x": "testx",
  "y": "testy",
  "memo": [
    "{'braums': {'printing_data': {'customerFirstName': 'xxxxxx', 'orderNumber': 4495, 'customerLastName': 'xxxxxx'}}}",
    "{'braums': {'cancellation_reasons': {'who': 'xxxxxxx', 'when': '11/18/2020 8:02 PM', 'why': 'called to cancel'}}}"
  ],
  "z": "testz"
}

EDIT: Just notice the mistake in the classes. The point of them did not change.

Paul
  • 101
  • 2
  • 10
  • 1
    Are you sure this is the actual JSON? It looks like a double serialization. `braums` shouldn't be quoted, it should be an object. Where is this JSON coming from? – Jimi Apr 09 '21 at 14:41
  • @Jimi Yes, it is a correct representation. Here is the full JSON with the names changed to protect the inconent. :-) https://paste.ubuntu.com/p/r4MFHKzFCH/ – Paul Apr 09 '21 at 15:34
  • The strings in the paste a different from what you're showing here (and it's missing quotes). The question, though, is the same: how did that JObject got stringified and ended up in that `memo` array? Is it something you have control over? It doesn't usually happen by itself. -- Anyway, you could use a custom JsonSerializer: in `ReadJson()`, see when you have `JsonToken.StartArray`, verify that it's your `memo` property in the `Order` object and deserialize to a class object instead. The `memo` Type must be turned into a `List`, containing `braums` objects. [...] – Jimi Apr 09 '21 at 16:01
  • Of course, if case the object's Properties you can find in `memo` are consistent, i.e., if there's an *underlying logic* in that content and you know what that content is in advance. -- There's probably a reason why you have that format. Maybe it's a partial work, with values that don't have found their *place* yet or are meant to be used somewhere else by something else that is not related to the objects described here. So, possibly, you don't actually need to deserialize those string along with this JSON, hence a *double-pass* is the obvious alternative. That's something you should know. – Jimi Apr 09 '21 at 16:01
  • Apply `` to `Public Property memo As List(Of Braum)` where `EmbeddedLiteralConverter(Of T)` comes from [this answer](https://stackoverflow.com/a/56137762) to [Deserialize JSON string with child objects embedded in a string value](https://stackoverflow.com/q/56136560) and also [this answer](https://stackoverflow.com/a/39154630) to [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043). Do those answer your question? They are in c# not vb.net. – dbc Apr 09 '21 at 16:11
  • @Jimi The vendor has more than one customer. I am guessing the memo is a "catch-all" for properties that they do not what to change the schema endpoint. I am just guessing. We are Braums and the print labels, customer signature, cancel reason is extra data we wanted. I was trying to distill the JSON string down to the least amount needed to ask the question. As for the differences in the memo() string. The Unicode was discussed here https://stackoverflow.com/questions/66953477/how-to-configure-class-definition-to-consume-unicode-marked-json, so I left them off. Just not seeing the quote issue. – Paul Apr 09 '21 at 16:38
  • Yep, I thought that could be the case. So, you probably want to create a model that includes your personal / couldn't find a place properties using a `Braums` class object and deserialize using a custom converter as mentioned - and how @dbc is suggesting - so you have a fully operational model that includes your personalization of the JSON. -- The code that converts those properties to a mangled JSON string is not doing its job correctly, IMO. You should probably talk to those who created this patch, so you can avoid to handle unwanted artifacts. – Jimi Apr 09 '21 at 17:08
  • Connected some dots with @dbc and jimi said. This fiddler is a working example of what I heard. https://dotnetfiddle.net/32bbuC dbc if rephrase your above comment as an answer I can give you credit. I felt there had to be a way to pre/post process, being new to JSON.net, I did not know how to ask it.. – Paul Apr 09 '21 at 18:02
  • @Jimi I see what you mean by quotes. I processing along until I hit this order. Here is the relevant snippet. https://paste.ubuntu.com/p/fcxhCTBFj5/ This time the TokenType was a StartObject. So now I know to test for the type of string first and then I can deserialize it to the object. I currently contacting the vendor to see if this is a one-off. I may still try and loop with the read() to pull it in. Will see how that goes. :-) One more dot connected. – Paul Apr 09 '21 at 19:11

0 Answers0