question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to parse json with mixed lists?

See original GitHub issue

At 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:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sebastiancommented, May 11, 2020

Thank you @NicoVIII! I really appreciate your detailed answer and the links! A year or not, I still learnt lots reading this!

0reactions
NicoVIIIcommented, May 11, 2020

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?

// Untested - call it Pseudocode
match something with
| :? string as value -> String value
| :? int as value -> Integer value
...

(https://fsharpforfunandprofit.com/posts/match-expression/#matching-on-subtypes)

Maybe this helps when people in the future need something like that again…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found