Search code examples
f#f#-interactivetype-providersf#-data

JsonProvider "This is not a constant expression or valid custom attribute value"


Given the code:

#if INTERACTIVE
#r "bin\Debug\FSharp.Data.dll"

#endif

open System
open FSharp.Data
open FSharp.Data.Json

let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }"""

//here is where i get the error
let Schema = JsonProvider<testJson>

The last line keeps giving me the error "This is not a constant expression or valid custom attribute value"-- what does that mean? How can i get it to read this JSON?


Solution

  • The string has to be marked as a constant. To do that, use the [<Literal>] attribute. Also, the type provider creates a type, not a value, so you need to use type instead of let:

    open FSharp.Data
    
    [<Literal>]
    let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }"""
    
    type Schema = JsonProvider<testJson>