Aaron M Aaron M - 5 months ago 12
JSON Question

Parsing out a portion of a JSON file and seeing the data

I found this F# JSON parsing - How to get property using complex path (consisting of several propery names) (I shortened the file string). I am wondering how to show the actual data from the json file in the FSI window?

let soilTest = JsonValue.Load("c:\\,,,soiltest.json")
let query soilTest=
let node = JObject.Parse soilTest
node.SelectToken "person[0].name" |> string


Here is the beginning of the json file:

{
"person": [
{
"name": "John Doe",
"date": "December 1, 2015",
.....


I looked at the link that Mark Seemann provided to see how to get the data out of the json file and be shown in the console.

However when putting

Console.WriteLine(name)


I get this warning:

warning FS0020: This expression should have type 'unit', but has type 'string'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.

val query : soilTest:string -> unit


Tried |>ignore at the end of console.writeline and I get val query : soulTest:string -> unit

How do you do a let statement from this point to bind the data to it?

Answer

soilTest is a of type JsonValue but you're trying to parse it as string, hence the error. Please adjust the path for your environment, otherwise this works for me:

let soilTest = JsonValue.Load(@"c:\tmp\test.json")
let qry (soilTest:JsonValue) = 
    let data = JObject.Parse (soilTest.ToString()) 
    data.SelectToken "person[0].name" |> string

qry soilTest
//val it : string = "John Doe"

There might be better ways to work with. Json and xml are the bane of my existence...