navicore / navipath   4.1.3

Apache License 2.0 GitHub

A library of JsonPath implicit convenience methods

Scala versions: 2.13 2.12

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 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 "<json>" is a valid json string or parsed output from .asJson:

    import navicore.data.navipath.dsl.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 navicore.data.navipath.dsl.NaviPathSyntax._
    val result = jsonString.query[String]("$.name")
    result should be ('defined)
    result.fold()(assertResult("Ishmael"))

Multiple matches support:

    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:

    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")
    ...
    ...