Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/navicore/navipath

A library of JsonPath implicit convenience methods
https://github.com/navicore/navipath

json jsonpath scala

Last synced: 8 days ago
JSON representation

A library of JsonPath implicit convenience methods

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/navicore/NaviPath.svg?branch=master)](https://travis-ci.org/navicore/NaviPath)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/70e6c4da5022432ab78cc212ed55759e)](https://www.codacy.com/app/navicore/NaviPath?utm_source=github.com&utm_medium=referral&utm_content=navicore/NaviPath&utm_campaign=Badge_Grade)

NaviPath
-----

A lib for adding implicit [JsonPath](http://jsonpath.com/) functions to Strings
======

Based on https://github.com/gatling/jsonpath. I've embedded the code from gatling instead of adding the maven binary dependency in order to support more scala versions via cross compile.

See http://goessner.net/articles/JsonPath/ for jsonpath documentation.

## INSTALL

* ongoing dev is for for scala 2.12+
* if you use scala 2.11 - use version 2.1.0

```
// https://mvnrepository.com/artifact/tech.navicore/navipath
libraryDependencies += "tech.navicore" %% "navipath" % "4.1.3"
```

## USAGE

See http://goessner.net/articles/JsonPath/ for JsonPath documentation.

### DSL USAGE

Examples where "\" is a valid json string or parsed output from `.asJson`:
```scala
import navicore.data.navipath.dsl.NaviPathSyntax._
"".query[String]("$.name")
"".query[Long]("$.widget.window.height")
"".query[List[String]]("$.stuff[*].name")
"".query[List[Int]]("$.stuff[*].value")
```

First match support:
```scala
val jsonString = """{"name": "Ishmael"}"""
import navicore.data.navipath.dsl.NaviPathSyntax._
val result = jsonString.query[String]("$.name")
result should be ('defined)
result.fold()(assertResult("Ishmael"))
```

Multiple matches support:
```scala
val jsonString = """{"stuff": [{"name": "Ishmael"}, {"name": "Mud"}]}"""
import navicore.data.navipath.dsl.NaviPathSyntax._
val results = jsonString.query[List[String]]("$.stuff[*].name")
results.fold()(r => assert(r.head == "Ishmael"))
results.fold()(r => assert(r(1) == "Mud"))
```

Parse once, query many times support:
```scala
val jsonString = """{"stuff": [{"name": "Ishmael", "id": 1}, {"name": "Mud", "id": 2}]}"""
import navicore.data.navipath.dsl.NaviPathSyntax._
val parsedJson = jsonString.asJson
val names = parsedJson.query[List[String]]("$.stuff[*].name")
val ids = parsedJson.query[List[Int]]("$.stuff[*].value")
...
...
```