I would like to parse JSON-structured data contained in a
let value = JsonValue.Load("C:/Users/mauri/OneDrive/Programmazione1/Sharpstone/SharpStone/Deck1")
for col in value?collectible do
printfn "%s" (col.AsString())
System.Exception: Not an object
The error message "Not an object" means that you are trying to access a property of a JSON node that is not an object (with fields) but something else - such as collection.
Using the sample deck from GitHub, I suppose that
value represents a collection and so writing
value?collectible will give you this error - because you are trying to access a property
collectible of something that is a collection.
The following (printing all artist names) should work:
let value = JsonValue.Load("https://raw.githubusercontent.com/Maurizio-Mario/Sharpstone/master/SharpStone/deck1.txt") for col in value.AsArray() do if col?collectible.AsBoolean() then printfn "%s" (col?artist.AsString())
That said, it is much easier to use the JSON type provider, which infers the type of the JSON document from a sample and then exposes all the properties in a type safe way:
type Deck = JsonProvider<"https://raw.githubusercontent.com/Maurizio-Mario/Sharpstone/master/SharpStone/deck1.txt"> let value = Deck.GetSamples() for col in value do if col.Collectible then printfn "%s" col.Artist