55640

F# Data: JSON Parser. Using JsonExtensions

Question:

It is my first question on SO...so do not judge strictly =)

Usually all my questions techout in <a href="http://chat.stackoverflow.com/rooms/51909/f" rel="nofollow">chat rooms</a> (believe me, a lot of them =)).

Recently, we are talking about the <a href="http://rosettacode.org/" rel="nofollow">RosettaCode</a>. And I wondered to complement some of the tasks code to F#

One of them is <a href="http://rosettacode.org/wiki/JSON" rel="nofollow">JSON</a>.

One of the possible solutions is the use of "F# Data: JSON Parser". So my question is linked with it.

This code works well:

open FSharp.Data open FSharp.Data.JsonExtensions type Person = {ID: int; Name:string} let json = """[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]""" json |> printfn "%s" match JsonValue.Parse(json) with | JsonValue.Array(x) -> x |> Array.map(fun x -> {ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}) | _ -> failwith "fail json" |> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)

Print:

[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }] 1 "First" 2 "Second"

But it

{ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}

doesn't look good.

<a href="http://fsharp.github.io/FSharp.Data/library/JsonValue.html" rel="nofollow">This</a> I read about JsonExtensions,

but when I use

{ID = (x?ID.AsInteger()) ; Name = (x?Name.AsString()) }

I get compile errors:

<ul><li>

The field, constructor or "AsInteger" is not defined

</li> <li>

The field, constructor or "AsString" is not defined

</li> </ul>

Strangely, thing is that I see accessibility through "open FSharp.Data.JsonExtensions"

<a href="https://i.stack.imgur.com/vSUNG.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/vSUNG.png" data-original="https://i.stack.imgur.com/vSUNG.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

So, question: How to use JsonExtensions?

Answer1:

I tried to reproduce this using a minimal example, but I do not get the error - can you try the following minimal sample?

#r "...../FSharp.Data.dll" open FSharp.Data.JsonExtensions open FSharp.Data JsonValue.Parse("A").AsArray() |> Array.map (fun a -> a?ID.AsInteger())

I do not get auto-completion on a?ID. (which is a limitation of the editor), but it compiles fine.

The only reason why I think this could be not working is if you had another open declaration that would import another implementation of the ? operator that is not returning JsonValue.

The JsonValue API is certainly not as nice as just using the type provider - so if you can, I'd probably go for the type provider instead (the low-level API is good if you need to iterate over everything in JSON recursively).

Recommend

  • Can F# Quotations be used to create a function applicable to arbitrary F# record types?
  • Array.create and jagged array
  • F# R Provider: Method not found: 'RDotNet.REngine RDotNet.REngine.GetInstance
  • FSharp.Data Csv.Load() failed with Only web locations are supported
  • F# cross-thread UI exception in WinForms App
  • Is there a way of querying or accessing the structure of a Z3 expression via the API
  • Ruby Mixins and Instance variables
  • Can this sql query be simplified?
  • Wordpress plugin for dependent drop down lists
  • Custom String Length Comparator: what's my mistake?
  • Convert row values into columns in R
  • How can I embed schema.org markup in SVG?
  • Storing Data in a Variable vs Inline Arithmetic
  • Error “An array may not have elements of this type”
  • How to get boolean property with expression language?
  • Purescript types for buildQueryString function
  • Alfresco - Get username in workflow
  • How to do JOIN ON query using Criteria API
  • Mockery and Laravel constructor injection
  • How to access recipient on sent messages page with mailboxer
  • Looking for good analogy/examples for monitor verses semaphore
  • Play WS (2.2.1): post/put large request
  • Different response to non-authenticated users and AJAX calls
  • Weird JavaScript statement, what does it mean?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal