navicore / navipath

A library of JsonPath implicit convenience methods

GitHub

Build Status Codacy Badge

NaviPath

A lib for adding implicit JsonPath 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 maintain 2.11 support.

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

INSTALL

  • for scala 2.11 and 2.12
// https://mvnrepository.com/artifact/tech.navicore/navipath
libraryDependencies += "tech.navicore" %% "navipath" % "0.1.7"

USAGE

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

DSL V1 USAGE

query path [<RESULT TYPE>] "<JSON PATH>" in "<JSON STRING>"

    val jsonString = """{"name": "Ishmael"}"""
    import onextent.data.navipath.dsl.NaviPathDslV1._
    val result = query path [String] "$.name" in jsonString
    result.fold()(assertResult("Ishmael"))

DSL V2 USAGE

Examples where "<json>" is a valid json string or parsed output from .asJson:

    import NaviPathSyntax._
    "<json>".query[String]("$.name")
    "<json>".query[Long]("$.widget.window.height")
    "<json>".query[List[String]]("$.stuff[*].name")
    "<json>".query[List[Int]]("$.stuff[*].value")

First match support:

    val jsonString = """{"name": "Ishmael"}"""
    import NaviPathSyntax._
    val result = jsonString.query[String]("$.name")
    result.fold()(assertResult("Ishmael"))

Multiple matches support:

    val jsonString = """{"stuff": [{"name": "Ishmael"}, {"name": "Mud"}]}"""
    import 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:

    val jsonString = """{"stuff": [{"name": "Ishmael", "id": 1}, {"name": "Mud", "id": 2}]}"""
    import NaviPathSyntax._
    val parsedJson = jsonString.asJson
    val names = parsedJson.query[List[String]]("$.stuff[*].name")
    val ids = parsedJson.query[List[Int]]("$.stuff[*].value")
    ...
    ...

OPS

publish local

sbt +publishLocalSigned

publish to nexus staging

sbt +publishSigned
sbt sonatypeReleaseAll