Is there an aeson equivalent in Elixir?

Just making sure I am not missing some corner of hex that has sophisticated json parsing/validation?

A coworker has been making elegant use of Ecto changeset on json. I assume that’s the best parsing/validating option for json data?

Some other tools, depending on what you need:

  • poison supports decoding directly into structs with the to: option

  • ex_json_schema supports validating structures using JSON Schema; useful if you want to return detailed errors about exactly what is wrong with the input JSON

One thing that neither of these tools will do is type-casting - for instance, timestamps are left as strings just as in the JSON input.

5 Likes

Self promotion, I have a jsonschema parser that is highly compile-time optimized: Exonerate — exonerate v0.2.2

5 Likes

I want the typecasting and more control than jsonschema.

You can always write your own, I wrote this in 30 minutes for a POC before just using json schema:

You can expand it to cast and validate however you like.

I find Ecto schemaless changesets to be a bit of a pain to use when you have to validate nested data, so I’m curious to see how you people are using it :slight_smile:

2 Likes

I would also have something to offer. :slightly_smiling_face:
Xema a schema validator inspired by JSON Schema.

defmodule Demo do
  use Xema

  xema do
    map(
      keys: :atoms,
      properties: %{
        pos: {:integer, minimum: 0},
        neg: {:integer, maximum: 0},
        created_at: {:struct, module: DateTime}
      }
    )
  end
end

Demo.cast(%{"pos" => "5", "neg" => "-5", "created_at" => "2022-08-27 00:00:00Z"})
#=> {:ok, %{created_at: ~U[2022-08-27 00:00:00Z], neg: -5, pos: 5}}

Demo.cast(%{"pos" => "5", "neg" => "99", "created_at" => "2022-08-27 00:00:00Z"})
#=> {:error, %Xema.ValidationError{reason: %{properties: %{neg: %{maximum: 0, value: 99}}}}}
2 Likes

I like json schema and more declarative validation. The only reason that I am not looking for it right now is because the current API that we are working with is implemented by a mediocre vendor which doesn’t publish a json schema, nor do I feel like I can trust the shape of json returned enough to infer the schema. I was hoping that we would implement very defensive code and try to recover from unexpected missing fields that are not critical, in addition to failing for completely invalid json.

I am going to try the following combination:

  1. read into struct with jason
  2. pass result through a “repair” function which puts in default values when non critical fields are missing or invalid, and returns a list of warning strings for each repaired field
  3. use xema to finish validating critical fields and casting
1 Like

I ran out of time and defaulted to just using primitive functions to validate. I looked at more examples of nested embedded schema validation with ecto. It looks okay, so I am neutral between Xema and Ecto. I will try to experiment more with both of them.

1 Like