Any way of JSON encoding an array with different types for items?

Hi all,

This is the JSON I need to post on the API

[ [ "a", "=", "val"]
, ["b", ">", 6 ]

Basically, an array of 3 items arrays that the last value can be any primitive type.

I’m kind of stock looking at JSON.Encode.list and .array not sure I can achieve this.

Any pointers would be highly appreciated. I would guess there’s a way to get that result?


Yes, you could use a custom type for the values in the list. Something like this

You could probably do something like this ?

import Json.Encode as E

E.list identity [ E.string "b", E.string ">", 6 ]

A nicer implementation using a type for the operations


Thanks @Sebastian this really helped.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.