0

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?

Community
  • 1
  • 1
Aaron M
  • 326
  • 2
  • 13
  • First, it's possible that `query` is not a reserved word, but I would still avoid binding to it. Just use `qry`. What is `name` bound to? In the last line of your function you might want to say `let name = node.SelectToken "person[0].name" |> string` then on the next line say `Console.WriteLine(name) |> ignore`. But generally you should be able to do pretty much anything with Newtonsoft.JSON...and/or the json provider. – s952163 Jun 29 '16 at 23:23
  • When i alter or remove query I get this error FS0001: This expression was expected to have type string but here has type > JsonValue . – Aaron M Jun 29 '16 at 23:36
  • how do you alter/remove the query? can you show the whole code, including whatever open statements you have, what you execute and the error. Can you come by http://chat.stackoverflow.com/rooms/51909/f to avoid going back and forth in the comments – s952163 Jun 29 '16 at 23:45
  • 1
    btw, you can avoid the whole `JObject.Parse` and access the name directly: `soilTest.["person"].[0].["name"] |> string`. – s952163 Jun 30 '16 at 01:00
  • Simpler code is better thanks again – Aaron M Jun 30 '16 at 01:05

1 Answers1

4

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...
Edit: e.g. you can access the properties directly, like this:

let soilTest = JsonValue.Load(@"c:\tmp\test.json")  
soilTest.["person"].[0].["name"].AsString()
//val it : string = "John Doe"
s952163
  • 6,276
  • 4
  • 23
  • 47