Is it possible to parse json with mixed lists?
See original GitHub issueAt first: Thanks for this library! I’m working with it to parse JSON responses from an API and it really works like a charm. If I correctly create types for the expected response, I can work type-safe with that data. But now the API sends inconsistent data, I get a list where complex objects and strings are mixed.
It looks like that:
{ "downloads": [ [ "English", {..} ] ] }
Is there some elegant way to handle such mixed lists with this library? I tried different approaches but none worked.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Go How do you deserialize a JSON array of mixed types
Is there a way to parse these items of mixed type? Can I possibly work around this by somehow forcing all the integer...
Read more >Handling json data with mixed structure - Request Feedback
In general, the best way to proceed is to first design an Elm data type that describes the data in a way that...
Read more >How to parse Json array with 2 or more different types ...
Basically the API i'm calling returns a json array as the root object. The two objects inside this array are different types. I...
Read more >Parsing complex JSON in Flutter
Rule #1 : Identify the structure. Json strings will either have a Map (key-value pairs) or a List of Maps. Rule #2 :...
Read more >Python Parse multiple JSON objects from file
To parse a JSON file with multiple JSON objects read one JSON object at a time and Convert it into Python dict using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you @NicoVIII! I really appreciate your detailed answer and the links! A year or not, I still learnt lots reading this!
Hi! I’m very sorry that I didn’t answer in time and am quite sure, that my answer now will not help you since it took me over a year…
I personally used Transforms to read out my information in a helpful way for me. You can have a look at that here: https://github.com/NicoVIII/GogApi.DotNet/commit/7a89344ca8da7164d7f4f0190031fd9887957c99
I’m not very experienced in F# (I only use it in hobby projects) so I’m not sure if this is the best way. But I guess a similar approach could work to get a list like you requested there:
Write a Transform from DecodedValue list into a obj list and write transformation functions for both directions (or only the one you need, I used only one direction, because I’m only reading) and add it to the field with
[<JsonField(Transform=typeof<...>)>]
.To transform the types I cast it one by one to the assumed type (for me the type at pos 1 was always the same). I’m not sure how to get the correct type here for the DecodedValue DU, but I guess you could use a type match approach?
(https://fsharpforfunandprofit.com/posts/match-expression/#matching-on-subtypes)
Maybe this helps when people in the future need something like that again…