3

The following code:

let resp = string(argv.GetValue 0)

let json =JObject.Parse resp

gives this error:

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' 
occurred in Newtonsoft.Json.dll

Input string '2.2.6' is not a valid number. 
Path 'items[0].versionName', line 1, position 39.

where argv is this input:

{  
     "totalCount":1,
     "items":[  
        {  
           "versionName":"2.2.6",
           "phase":"PLANNING",
           "distribution":"EXTERNAL",
           "source":"CUSTOM",
           "_meta":{  
              "allow":[  
                 "GET",
                 "PUT",
                 "DELETE"
              ],
              "href":"url",
              "links":[  
                 {  
                    "rel":"versionReport",
                    "href":"url"
                 },
                 {  
                    "rel":"project",
                    "href":"url"
                 },
                 {  
                    "rel":"policy-status",
                    "href":"url"
                 }
              ]
           }
        }
     ]
  }

How can I fix this? Is there a simple way to implement a json reader that does not error here?

I also get this error:

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' 
occurred in Newtonsoft.Json.dll

Error parsing undefined value. Path 'items[0].name', line 1, position 28.

With this input:

{  
    "totalCount":1,
    "items":[  
        {  
            "name":"uaa",
            "projectLevelAdjustments":true,
            "source":"CUSTOM",
            "_meta":{  
                "allow":[  
                    "GET",
                    "PUT",
                    "DELETE"
                ],
                "href":"url",
                "links":[  
                    {  
                        "rel":"versions",
                        "href":"url"
                    },
                    {  
                        "rel":"canonicalVersion",
                        "href":"url"
                    }
                ]
            }
        }
    ]
}

I am trying to read in json of many different schemas that I did not make or know. The first error seems to be because it is trying to generate a float from something that should be output as a string. The second sounds like the schema is too complex and a type would be needed to properly parse it using Json.Deserialize but I'm not sure how to do that and it would take too much time as there are too many schemas to make a types for them all. Is there any way around both these things?

Grayden Hormes
  • 855
  • 1
  • 15
  • 34
  • 2
    strange, but your code not gives exception for me – FoggyFinder Oct 18 '16 at 08:06
  • 2
    Yea I'm not getting any exception. The code you should be using is `let json = JsonConvert.DeserializeObject(resp)`. And then you can get values with the example I have above. If it isn't working then you should provide us a json sample that crashes. – Ringil Oct 18 '16 at 11:22
  • added edit 2 to my answer, this issue is getting perplexing if no one else is getting exceptions :/ – Grayden Hormes Oct 18 '16 at 18:45
  • I'm doing it by just having it as a raw string in my program works, I think it was just passing it in by the command line arguments not working, sorry for the confusion. – Grayden Hormes Oct 18 '16 at 19:17
  • so, your problem still not solved? – FoggyFinder Oct 18 '16 at 20:06
  • 1
    I was checking the source of `JSON.NET` and I struggling to understand how you could get that error from that particular JSON document by using `JObject.Parse`. Not saying it's impossible but I couldn't construct the scenario where it happened. – Just another metaprogrammer Oct 18 '16 at 20:21
  • yes Ringil's answer works – Grayden Hormes Oct 18 '16 at 20:32

1 Answers1

3

In C# you can use dynamic with something like this:

var json = JsonConvert.DeserializeObject(resp)

And then you could access properties with something like: json.totalCount.

In F# land, this question gives some suggestions for how you might deal with dynamic objects. If you use the package FSharp.Interop.Dynamic you can get the value of totalCount in your example with something like this:

let value:obj = json?totalCount?Value

which gives 1L on my computer.

Community
  • 1
  • 1
Ringil
  • 6,277
  • 2
  • 23
  • 37