GithubHelp home page GithubHelp logo

pratikpparikh / jslt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from schibsted/jslt

0.0 2.0 0.0 746 KB

JSON query and transformation language

License: Apache License 2.0

HTML 1.08% Shell 0.15% Java 98.77%

jslt's Introduction

JSLT

JSLT is a complete query and transformation language for JSON. The language design is inspired by jq, XPath, and XQuery.

JSLT can be used as:

  • a query language to extract values from JSON (.foo.bar[0]),
  • a filter/check language to test JSON objects (starts-with(.foo.bar[0], "http://")) ,
  • a transformation language to convert between JSON formats.

Here is an example transform:

{
    "time": round(parse-time(.published, "yyyy-MM-dd'T'HH:mm:ssX") * 1000),
    "device_manufacturer": .device.manufacturer,
    "device_model": .device.model,
    "language": .device.acceptLanguage,
    "os_name": .device.osType,
    "os_version": .device.osVersion,
    "platform": .device.platformType,
    "user_properties": {
        "is_logged_in" : boolean(.actor."spt:userId")
    }
}

Demo playground.

Language tutorial.

Function documentation.

More examples.

Build Status

Quick reference

Operation Explanation
. The context node
.<name> Get value of key "<name>" inside an object
.[<index>] Get value <index> inside an array
.[<from> : <to>] Array slicing
if (<expr>) <expr> else <expr> If test to decide which value to return
let <name> = <expr> Define a variable
$<name> Refer to a variable
[for (<expr>) <expr>] Transform an array
{for (<expr>) <expr> : <expr>} Transform an object
def <name>(<name>, <name>...) <expr> Declare a function
// <anything up to end of line> Comment
{ <key> : <expr> } Object constructor
{ <key> : <expr>, * : . } Specify one key, copy rest of input

Using the library

To include JSLT in your project, depend on:

<dependency>
  <groupId>com.schibsted.spt.data</groupId>
  <artifactId>jslt</artifactId>
  <version>0.1.3</version>
</dependency>

At runtime JSLT depends on Jackson, and nothing else.

To transform one JsonNode into another, do:

import com.schibsted.spt.data.jslt.Parser;
import com.schibsted.spt.data.jslt.Expression;

JsonNode input = ...;
Expression jslt = Parser.compileString(transform);
JsonNode output = jslt.apply(input);

For more alternatives, see the javadoc.

Command-line

To run transforms on the command-line, first build with ./gradlew clean shadowJar. Then you can run with:

java -cp build/libs/*.jar com.schibsted.spt.data.jslt.cli.JSLT transform.jslt input.json

The result is written to standard out.

Extension functions

You can implement your own functions and add them to the language. See the extension function tutorial.

Feedback

If you have questions about how to use JSLT, please ask the question on StackOverflow, with the tag jslt.

If you have problems, feature requests, or think you found a bug, please open an issue.

Status

The language design is not finished, so features may be added. The language as it stands is not likely to change.

The entire language is implemented, and all of the function library. Functions may be added.

The language has been used in production at Schibsted since January 2018, performing about 9 billion transforms per day, and many times more queries.

Other implementations

Anthony Sparks is working on a VM-based implementation in Java

LICENSE

Copyright (c) 2018 Schibsted Marketplaces Products & Technology AS

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

What is missing

Things to be done:

  • Move the tests out into JSON files.
  • Write a proper spec with EBNF and everything.
  • Fix the syntax ambiguity problem with let and def.
  • Implement toString() throughout the object tree, so that it's possible to turn expressions back to strings.
  • Optimizer:
    • Optimize ... and boolean( ... ) by removing boolean().
    • Implement parse tree traversal API.
    • Make sure entire tree is traversed (inside function decls and variables, for example).
    • Complete constant folding. Particularly constant folding for variables would be valuable.
    • Inlining of functions.
    • Eliminate unused variables.
  • Avoid building more scope objects than necessary and avoid creating deep scope trees that require a lot of lookup.
  • Use property-based testing and fuzz testing to harden the parser.

See also the list of ideas.

jslt's People

Contributors

larsga avatar pratikpparikh avatar vectro avatar

Watchers

 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.