https://github.com/purescript-contrib/purescript-argonaut-traversals
Prisms, traversals, and zipper for the Argonaut Json type.
https://github.com/purescript-contrib/purescript-argonaut-traversals
Last synced: 3 months ago
JSON representation
Prisms, traversals, and zipper for the Argonaut Json type.
- Host: GitHub
- URL: https://github.com/purescript-contrib/purescript-argonaut-traversals
- Owner: purescript-contrib
- License: mit
- Created: 2015-07-10T18:07:36.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2022-04-27T23:09:45.000Z (almost 4 years ago)
- Last Synced: 2025-02-08T20:01:42.553Z (about 1 year ago)
- Language: PureScript
- Homepage:
- Size: 113 KB
- Stars: 11
- Watchers: 9
- Forks: 16
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Argonaut Traversals
[](https://github.com/purescript-contrib/purescript-argonaut-traversals/actions?query=workflow%3ACI+branch%3Amain)
[](https://github.com/purescript-contrib/purescript-argonaut-traversals/releases)
[](http://pursuit.purescript.org/packages/purescript-argonaut-traversals)
[](http://github.com/garyb)
Prisms, traversals, and zipper for the `Json` type. Part of the [Argonaut](https://github.com/purescript-contrib/purescript-argonaut) collection of libraries for working with JSON in PureScript.
## Installation
Install `argonaut-traversals` with [Spago](https://github.com/purescript/spago):
```sh
spago install argonaut-traversals
```
or install as part of the Argonaut bundle:
```sh
spago install argonaut
```
## Quick start
You can use the prisms defined in `Data.Argonaut.Prisms` with functions from the [`profunctor-lenses`](https://github.com/purescript-contrib/purescript-profunctor-lenses) library to work with nested `Json` structures. For example:
```js
// FFI file
exports.sampleJson = { "a": { "b": [ 10, 11, 12 ] } }
```
```purs
module Main where
import Prelude
import Effect (Effect)
import Data.Argonaut.Core (Json)
import Data.Argonaut.Prisms (_Array, _Number, _Object)
import Data.Maybe (Maybe(..))
import Data.Lens (preview)
import Data.Lens.Index (ix)
import Effect.Console (log)
foreign import sampleJson :: Json
main :: Effect Unit
main =
-- Walk through an object at the key 'a', then an object at the key 'b', then
-- get the first index of an array as a number.
case preview (_Object <<< ix "a" <<< _Object <<< ix "b" <<< _Array <<< ix 0 <<< _Number) sampleJson of
Nothing -> log "nothin' there"
Just v -> log $ "This should be 10.0 " <> show v
```
You may also be interested in other libraries in the Argonaut ecosystem:
- [purescript-argonaut-core](https://github.com/purescript-contrib/purescript-argonaut-core) defines the `Json` type, along with basic parsing, printing, and folding functions
- [purescript-argonaut-codecs](https://github.com/purescript-contrib/purescript-argonaut-codecs) provides codecs based on `EncodeJson` and `DecodeJson` type classes, along with instances for common data types and combinators for encoding and decoding `Json` values.
- [purescript-codec-argonaut](https://github.com/garyb/purescript-codec-argonaut) supports an alternative approach for codecs, which are based on profunctors instead of type classes.
- [purescript-argonaut-generic](https://github.com/purescript-contrib/purescript-argonaut-generic) supports generic encoding and decoding for any type with a `Generic` instance.
## Documentation
`argonaut-traversals` documentation is stored in a few places:
1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-argonaut-traversals).
2. Written documentation is kept in [the docs directory](./docs).
3. Usage examples can be found in [the test suite](./test).
If you get stuck, there are several ways to get help:
- [Open an issue](https://github.com/purescript-contrib/purescript-argonaut-traversals/issues) if you have encountered a bug or problem.
- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat.
## Contributing
You can contribute to `argonaut-traversals` in several ways:
1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-argonaut-traversals/issues). We'll do our best to work with you to resolve or answer it.
2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions.
3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed.