How to use Data.Argonaut.Decode module with JSON with any field?

Viewed 62

I know if I have JSON with specific known fields, like "date" and "title"

{
  "date": "any string",
  "title": "any string"
}

Then, I can decode it by defining

myDecodeFunc :: Json -> Either JsonDecodeError ({ date :: String, title :: String })

myDecodeFunc = decodeJson

However, I can't seem to find how to decode the same JSON with arbitrary fields like this.

{
  "any field": "any string",
  "any field": "any string"
}

I am new to Purescript, so any pointers would be appreciated. Thanks.

1 Answers

A fancy term for a "record"-ish thing where you don't know all possible fields in advance is dictionary.

And to parse JSON as a dictionary, use a dictionary-ish type, such as Foreign.Object for example, which just happens to have a handy instance of FromJSON:

import Foreign.Object (Object)

myDecodeFunc :: Json -> Either JsonDecodeError (Object String)
myDecodeFunc = decodeJson 
Related