F# how to loop the Json JObject array

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
Hello:

I have to parse and extract useful information from a rather complicated Json structured data. The following is just a small part of the Json data:

{{

"offset": 0,

"per-page": 1,

"total": 1,

"events": [

{

"id": 14,

"name": "Team A at Team B",

"sport-id": 14,

"start": "2018-04-15T19:35:00Z",

"in-running-flag": true,

"all-live": true,

"category-id": [

14,

40620231567,

41046852088,

69459601606

],

"status": "open",

"volume": 10.01,

"event-participants": [

{

"id": 14,

"event-id": 14,

"participant-name": "Team A"

},

{

"id": 15,

"event-id": 14,

"participant-name": "Team B"

}

],

"markets": [

{

"live": true,

"event-id": 14,

"id": 14,

"name": "line",

"runners": [

{

"withdrawn": false,

"prices": [

{

"available-amount": 213.01,

"currency": "USD",

"rate-type": "DECIMAL",

"rate": 1.17857,

"decimal-rate": 1.17857,

"side": "b",

"exchange-type": "binary"

},

],

]

}}


I used some online tools to analyse the structure of the Json data, but it is rather complicated. I wanted to use the old way to parse the Json data, define a type with necessary information, but since the Json structure is rather complicated, it seems not an easy job, or even not possible to do so. The same web site has even much more complicated Json structured data to parse.

However, someone already parsed the exact same Json data in C#, as the code is rather lengthy, but I can see the important related part of the C# code is:

using System;
using System.Text;
using Newtonsoft.Json.Linq;
using Microsoft.VisualBasic;

JObject parsedJson = null;
parsedJson = JObject.Parse(st_RESPONSE);
/// st_RESPONSE is the response of http REST request
foreach (JObject EventX in parsedJson["events"])
{
...
Newtonsoft.Json.Linq.JArray arEventMarkets = (JArray)EventX["markets"];
foreach (Newtonsoft.Json.Linq.JObject MarketX in arEventMarkets)
{
....

Unlike the F# way for defining a Json type, which is almost impossible, as the actual data structure is quite complicated, the C# code simply loop each layer of Json data, and parse and get the necessary information.

I try to do the same, but I can’t figure out the first layer yet, the following is my code:

#light
open FSharp.Data
open Newtonsoft.Json
open Newtonsoft.Json.Linq
open RestSharp
open RestSharp.Deserializers
open System
open System.Collections

let client = new RestClient("https://someurl")
let request = new RestRequest(Method.GET)
request.AddHeader("Accept", "application/json") |> ignore
request.AddHeader("Content-Type", "application/json") |> ignore
request.AddParameter("states", "open") |> ignore
let response = client.Execute(request)

let parsedJson = JObject.Parse(response.Content)

Note: the response.Content is the same as st_RESPONSE in C# code.

How I can do the same job like in C#?

foreach (JObject EventX in parsedJson["events"])


The JObject array parsedJson["events"] simply didn’t exist in F#, but in C#, it existed.





Name


Value


Type




parseJson["events"]


error CS0103: The name 'parseJson' does not exist in the current context





Please advise, how I can loop the Json JObject array in Newtonsoft.Json, or if you know other JSon package for F# can do the same job, please share your code.

Thanks,

Continue reading...
 
Back
Top