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?

Thanks,
Dominic

Yes, you could use a custom type for the values in the list. Something like this https://ellie-app.com/7QjP7XSyZsfa1

You could probably do something like this ?

import Json.Encode as E

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

A nicer implementation using a type for the operations
https://ellie-app.com/7QjWPJTK24ca1

2 Likes

Thanks @Sebastian this really helped.

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