How to hide properties with null values in FSharp.Data JsonProvider?

110 Views Asked by At

This piece of code:

open FSharp.Data

type ColorProvider = JsonProvider<"""
[
  {
    "color": "Red",
    "code": 15
  },
  {
    "color": "Green"
  }
]
""", SampleIsList=true>

let value = ColorProvider.Root(color = "Blue", code = None)

printf "%A" value

Produces this JSON:

{
  "color": "Blue",
  "code": null
}

I'm passing the json to an external service which doesn't handle nulls. Either it must be an integer or the whole property must be missing. Is there a way to hide the code property when it's null?

1

There are 1 best solutions below

0
ajuch On

You are using FSharp's object printing feature for generating JSON. It produces JSON-like output, but in my opinion it is not meant for that task. I'd suggest using a JSON library for that, personally I like using https://github.com/Microsoft/fsharplu/wiki/fsharplu.json for that task, as it produces beautiful JSON for Discriminate Unions.

Related Questions in F#

Related Questions in F#-DATA