0

I get a JSON that is very dynamic so I can just simple deserialize it to a object. I need to change the logic depending in what keys it have and what kind of nested data it get.

In python is easy, because it turns a JSON in dict/list. But how do the same with Json.net?

mamcx
  • 15,916
  • 26
  • 101
  • 189
  • You can deserialize it to dynamic and then continue testing if it has your properties not null – Rey Jan 29 '18 at 23:34
  • I'm using F#. It not have dynamic – mamcx Jan 29 '18 at 23:38
  • Oh sorry. I was in the phone and did not see properly the tag f#. Thought it was c# – Rey Jan 29 '18 at 23:40
  • 1
    Then you can deserialize into [`JToken`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm), which is the underlying type actually returned when deserializing as `dynamic` in c#. See e.g. [JSON.NET: Why Use JToken--ever?](https://stackoverflow.com/q/38211719/3744182). – dbc Jan 29 '18 at 23:51
  • 1
    See also [mrange/F# Dynamic JSON.NET](https://gist.github.com/mrange/988ffdbeb75568aa4ed9) and [Parsing JSON Using F# (not Serialization)](https://stackoverflow.com/q/6361610/3744182) the latter of which is specific to JSON with an object as the root value. – dbc Jan 30 '18 at 00:00
  • JToken.parse still not deserialize the object as I expected, I get "Error FS0331" when used. – mamcx Jan 30 '18 at 00:02
  • 1
    @dbc I try https://gist.github.com/mrange/988ffdbeb75568aa4ed9 and it work for me. – mamcx Jan 30 '18 at 00:26
  • 1
    *JToken.parse still not deserialize the object as I expected, I get "Error FS0331" when used.* - we would need to see a [mcve] for that. – dbc Jan 30 '18 at 00:27
  • 1
    See also [How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](https://stackoverflow.com/q/5546142/10263) – Brian Rogers Jan 30 '18 at 03:00
  • It would help if you could post some code example of what's not working. However you can use dynamic in F#, please see http://www.nuget.org/packages/FSharp.Interop.Dynamic/ and also Dynamitey which it uses. – s952163 Jan 30 '18 at 04:48

0 Answers0