Code Monkey home page Code Monkey logo

java-jq's Introduction

java-jq

java-jq is not a re-implementation of jq in Java; instead, it embeds the necessary jq and Oniguruma native libraries in a jar file, and then uses Java Native Access (JNA) to call the embedded libraries in a Java-friendly way.

The distribution of java-jq includes native JQ 1.6 libraries for all major platforms (Mac, Windows and Linux), and includes a statically linked version of Oniguruma 5.9.6 to avoid any runtime compatibility issues.

java-jq was heavily inspired by jjq.

Usage

Using Java-JQ is very easy.

First, let's get a reference to the Native JQ library. This class is a thread-safe singleton.

JqLibrary library = ImmutableJqLibrary.of();

Now, let's create a JQ request. A "request" is an immutable bean that contains three basic elements: a reference to the JQ library we created above, the JSON input you want to transform, and the JQ filter expression that you want to execute.

final JqRequest request = ImmutableJqRequest.builder() //
        .lib(library) //
        .input("{\"a\":[1,2,3,4,5],\"b\":\"hello\"}") //
        .filter(".") //
        .build();

As a final step, let's execute the request.

final JqResponse response = request.execute();
if( response.hasError ) {
   // display errors in response.getErrors()
} else {
   System.out.println( "JQ output: " + response.getOutput);
}

Compatibility

As of version 1.1.0, java-jq successfully executes the complete jq test suite, including all tests in jq.test, onig.test, base64.test, and optional.test.

java-jq supports modules as well. To use modules, include the directory paths where your modules can be found with your JqRequest as follows:

final JqRequest request = ImmutableJqRequest.builder() //
        .lib(library) //
        .input("your json goes here") //
        .filter(".") //
        .addModulePath(new File("/some/modules/can/be/found/here")) //
        .addModulePath(new File("/other/modules/can/be/found/here")) //
        .build();

Installation

The library is available on Maven Central.

Maven

Add the following to your pom.xml:

<repositories>
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

...

<dependency>
    <groupId>com.arakelian</groupId>
    <artifactId>java-jq</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

Gradle

Add the following to your build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  testCompile 'com.arakelian:java-jq:1.1.0'
}

Licence

Apache Version 2.0

java-jq's People

Contributors

arakelian avatar yurikpanic avatar

Watchers

James Cloos 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.