https://github.com/gleam-lang/json
🐑 Work with JSON in Gleam!
https://github.com/gleam-lang/json
gleam json
Last synced: 20 days ago
JSON representation
🐑 Work with JSON in Gleam!
- Host: GitHub
- URL: https://github.com/gleam-lang/json
- Owner: gleam-lang
- License: apache-2.0
- Created: 2021-12-28T15:53:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T10:47:59.000Z (about 1 year ago)
- Last Synced: 2024-04-08T11:48:28.270Z (about 1 year ago)
- Topics: gleam, json
- Language: Gleam
- Homepage: https://hexdocs.pm/gleam_json/
- Size: 60.5 KB
- Stars: 46
- Watchers: 5
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-gleam - gleam_json - [📚](https://hexdocs.pm/gleam_json/) - Work with JSON in Gleam (Packages / JSON)
README
# json 🐑
Work with JSON in Gleam!
## Installation
```shell
gleam add gleam_json@3
```## Encoding
```gleam
import myapp.{type Cat}
import gleam/jsonpub fn cat_to_json(cat: Cat) -> String {
json.object([
#("name", json.string(cat.name)),
#("lives", json.int(cat.lives)),
#("flaws", json.null()),
#("nicknames", json.array(cat.nicknames, of: json.string)),
])
|> json.to_string
}
```## Parsing
JSON is parsed into a `Dynamic` value which can be decoded using the
`gleam/dynamic/decode` module from the Gleam standard library.```gleam
import myapp.{Cat}
import gleam/json
import gleam/dynamic/decodepub fn cat_from_json(json_string: String) -> Result(Cat, json.DecodeError) {
let cat_decoder = {
use name <- decode.field("name", decode.string)
use lives <- decode.field("lives", decode.int)
use nicknames <- decode.field("nicknames", decode.list(decode.string))
decode.success(Cat(name:, lives:, nicknames:))
}
json.parse(from: json_string, using: cat_decoder)
}
```