Git Product home page Git Product logo

jp's Introduction

jp

A simple json-path parser for scala

install

...

usage

Parsing paths

The core functionality exposed in jp is json path parsing

import jp._

val path = Path("$.store.book[0].title")

Will return a Path object from which you may call result to get the corresponding ParseResult.

Transforming paths

To make results more useful in applications, jp exposes a trait called Transform[I,O] which transforms a type I into type O given the json path parse results.

Below is an example of transforming a json path parse result with lift json.

import net.liftweb.json.JsonAST
import net.liftweb.json.Printer

path("""{"store":"book":[{"title":"test"}]}""")(as.lift.Json).fold(identity, { js =>
   Printer.pretty(JsonAST.render(js))
})

Passing in a Transform will result in Either[String, T] where String contains an error message for a potential malform parsing result, and T represents the type of the transformation, in this, a case JValue.

reference

JSONPath OperatorDescription
$the root object/element
@the current object/element
. or []child operator
..recursive descent. JSONPath borrows this syntax from E4X
*wildcard. All objects/elements regardless their names.
[]subscript operator.
[,]Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
[start:end:step]array slice operator borrowed from ES4
?()applies a filter (script) expression
()script expression, using the underlying script engine

todo

  • (<expr>) expression evaluation $.store.book[(@.length-1)]
  • @ current object references (only useful in (<expr>) contexts)
  • ?(<bool expr>) filters $.store.book[?(@.price < 10)].title
  • [,] union operator $..book[0,1]

Doug Tangren (softprops) 2012

jp's People

Contributors

softprops avatar

Stargazers

 avatar Erik Osheim avatar

Watchers

 avatar kenji yoshida avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.