Code Monkey home page Code Monkey logo

jsonpathkt's Introduction

JsonPathKt

Build Status Maven Central codecov

A lighter and more efficient implementation of JsonPath in Kotlin. With functional programming aspects found in langauges like Kotlin, Scala, and streams/lambdas in Java8, this library simplifies other implementations like Jayway's JsonPath by removing filter operations and in-path functions to focus on what matters most: modern fast value extractions from JSON objects. Up to 4x more efficient in some cases; see Benchmarks.

In order to make the library functional programming friendly, JsonPathKt returns null instead of throwing exceptions while evaluating a path against a JSON object. Throwing exceptions breaks flow control and should be reserved for exceptional errors only.

Code examples

A jsonpath that exists returns that value. null is returned when it doesn't.

val json = """{"hello": "world"}"""
JsonPath.parse(json)?.read<String>("$.hello") // returns "world"
JsonPath.parse(json)?.read<String>("$.somethingelse") // returns null since "somethingelse" key not found

A jsonpath that returns a collection containing the 2nd and 3rd items in the list (index 0 based and exclusive at range end).

val json = """{"list": ["a","b","c","d"]}"""
JsonPath.parse(json)?.read<List<String>>("$.list[1:3]") // returns listOf("b", "c")

JsonPathKt also works with Map and POJO.

val json = """[{ "outer": {"inner": 1} }]"""
JsonPath.parse(json)?.read<Map<String, Int>>("$[0].outer") // returns mapOf("inner" to 1)
data class ParsedResult(val outer: Map<String, Int>) // define this class in file scope, not in function scope which will anonymize it 
JsonPath.parse(json)?.read<ParsedResult>("$[0]") // returns ParsedResult instance

Internally, a jsonpath is compiled into a list of tokens. You can compile a complex jsonpath once and reuse it across multiple JSON strings.

val jsonpath = JsonPath("$.family.children..['name','nickname']")
jsonpath.readFromJson<List<Map<String, String>>>(json1)
jsonpath.readFromJson<List<Map<String, String>>>(json2)

JsonPathKt uses Jackson to deserialize JSON strings. JsonPath.parse returns a Jackson JsonNode object, so if you've already deserialized, you can also read the jsonpath value directly.

Getting started

JsonPathKt is available at the Maven Central repository.

POM

<dependency>
  <groupId>com.nfeld.jsonpathkt</groupId>
  <artifactId>jsonpathkt</artifactId>
  <version>2.0.1</version>
</dependency>

Gradle

dependencies {
    implementation 'com.nfeld.jsonpathkt:jsonpathkt:2.0.1'
}

Accessor operators

Operator Description
$ The root element to query. This begins all path expressions.
.. Deep scan for values behind followed key value accessor
.<name> Dot-notated key value accessor for JSON objects
['<name>' (, '<name>')] Bracket-notated key value accessor for JSON objects, comma-delimited
[<number> (, <number>)] JSON array accessor for index or comma-delimited indices
[start:end] JSON array range accessor from start (inclusive) to end (exclusive)

Path expression examples

JsonPathKt expressions can use any combination of dot–notation and bracket–notation operators to access JSON values. For examples, these all evaluate to the same result:

$.family.children[0].name
$['family']['children'][0]['name']
$['family'].children[0].name

Given the JSON:

{
    "family": {
        "children": [{
                "name": "Thomas",
                "age": 13
            },
            {
                "name": "Mila",
                "age": 18
            },
            {
                "name": "Konstantin",
                "age": 29,
                "nickname": "Kons"
            },
            {
                "name": "Tracy",
                "age": 4
            }
        ]
    }
}
JsonPath Result
$.family The family object
$.family.children The children array
$.family['children'] The children array
$.family.children[2] The second child object
$.family.children[-1] The last child object
$.family.children[-3] The 3rd to last child object
$.family.children[1:3] The 2nd and 3rd children objects
$.family.children[:3] The first three children
$.family.children[:-1] The first three children
$.family.children[2:] The last two children
$.family.children[-2:] The last two children
$..name All names
$.family..name All names nested within family object
$.family.children[:3]..age The ages of first three children
$..['name','nickname'] Names & nicknames (if any) of all children
$.family.children[0].* Names & age values of first child

Benchmarks

These are benchmark tests of JsonPathKt against Jayway's JsonPath implementation. Results for each test is the average of 30 runs with 80,000 reads per run and each test returns its own respective results (some larger than others). You can run these tests locally with ./runBenchmarks.sh

Evaluating/reading path against large JSON

Path Tested JsonPathKt (ms) JsonPath (ms)
$[0].friends[1].other.a.b['c'] 88 ms (35 ms w/ cache) 144 ms (79 ms w/ cache)
$[2]._id 34 ms (14 ms w/ cache) 48 ms (28 ms w/ cache)
$..name 82 ms (84 ms w/ cache) 450 ms (572 ms w/ cache)
$..['email','name'] 116 ms (108 ms w/ cache) 479 ms (522 ms w/ cache)
$..[1] 203 ms (202 ms w/ cache) 401 ms (423 ms w/ cache)
$..[:2] 352 ms (346 ms w/ cache) 442 ms (437 ms w/ cache)
$..[2:] 426 ms (419 ms w/ cache) 476 ms (470 ms w/ cache)
$[0]['tags'][-3] 67 ms (17 ms w/ cache) 83 ms (37 ms w/ cache)
$[0]['tags'][:3] 90 ms (36 ms w/ cache) 103 ms (55 ms w/ cache)
$[0]['tags'][3:] 97 ms (48 ms w/ cache) 112 ms (65 ms w/ cache)
$[0]['tags'][3:5] 85 ms (29 ms w/ cache) 101 ms (50 ms w/ cache)
$[0]['tags'][0,3,5] 95 ms (36 ms w/ cache) 122 ms (52 ms w/ cache)
$[0]['latitude','longitude','isActive'] 97 ms (36 ms w/ cache) 124 ms (59 ms w/ cache)
$[0]['tags'].* 88 ms (47 ms w/ cache) 121 ms (86 ms w/ cache)
$[0]..* 828 ms (796 ms w/ cache) 797 ms (830 ms w/ cache)

Compiling JsonPath strings to internal tokens

Path size JsonPathKt JsonPath
7 chars, 1 tokens 14 ms (2 ms w/ cache) 9 ms (9 ms w/ cache)
16 chars, 3 tokens 26 ms (2 ms w/ cache) 25 ms (25 ms w/ cache)
30 chars, 7 tokens 55 ms (2 ms w/ cache) 63 ms (57 ms w/ cache)
65 chars, 16 tokens 114 ms (2 ms w/ cache) 157 ms (142 ms w/ cache)
88 chars, 19 tokens 205 ms (2 ms w/ cache) 234 ms (204 ms w/ cache)

Cache

JsonPathKt uses an LRU cache by default to cache compiled JsonPath tokens. If you don't want to use the cache, you can disable it or set the CacheProvider to use your own implementation of the Cache interface.

// Disable cache
CacheProvider.setCache(null)

// Implement your own cache
CacheProvider.setCache(object : Cache {
    override fun get(path: String): JsonPath? { ... }
    override fun put(path: String, jsonPath: JsonPath) { ... }
})

Analytics

jsonpathkt's People

Contributors

codeniko avatar cyber-barrista avatar dependabot[bot] avatar eygraber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

jsonpathkt's Issues

Results do not match other implementations

The following queries provide results that do not match those of other implementations of JSONPath
(compare https://cburgmer.github.io/json-path-comparison/):

  • $[:]
    Input:

    ["first", "second"]
    

    Expected output:

    ["first", "second"]
    

    Error:

    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project query: An exception occured while executing the Java class. Index 0 out of bounds for length 0 -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project query: An exception occured while executing the Java class. Index 0 out of bounds for length 0
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
        at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
        at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
        at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke (Method.java:566)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
    Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. Index 0 out of bounds for length 0
        at org.codehaus.mojo.exec.ExecJavaMojo.execute (ExecJavaMojo.java:339)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
        at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
        at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
        at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke (Method.java:566)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
    Caused by: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
        at jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:64)
        at jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:70)
        at jdk.internal.util.Preconditions.checkIndex (Preconditions.java:248)
        at java.util.Objects.checkIndex (Objects.java:372)
        at java.util.ArrayList.get (ArrayList.java:458)
        at com.nfeld.jsonpathlite.PathCompiler.compileBracket$json_path_lite (PathCompiler.kt:232)
        at com.nfeld.jsonpathlite.PathCompiler.compile$json_path_lite (PathCompiler.kt:59)
        at com.nfeld.jsonpathlite.JsonPath.<init> (JsonPath.kt:17)
        at com.nfeld.jsonpathlite.extension.JSONArrayKt.read (JSONArray.kt:8)
        at com.nfeld.jsonpathlite.JsonArray.read (JsonResult.kt:12)
        at query.AppKt.main (App.kt:10)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke (Method.java:566)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
        at java.lang.Thread.run (Thread.java:834)
    [ERROR] 
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
    
  • $['key','another']
    Input:

    {"another": "entry", "key": "value"}
    

    Expected output:

    ["value", "entry"]
    

    Actual output:

    {"key": "value", "another": "entry"}
    
  • $.store..price
    Input:

    {"store": {"book": [{"category": "reference", "price": 8.95, "title": "Sayings of the Century", "author": "Nigel Rees"}, {"category": "fiction", "price": 12.99, "title": "Sword of Honour", "author": "Evelyn Waugh"}, {"category": "fiction", "price": 8.99, "title": "Moby Dick", "isbn": "0-553-21311-3", "author": "Herman Melville"}, {"category": "fiction", "price": 22.99, "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "author": "J. R. R. Tolkien"}], "bicycle": {"color": "red", "price": 19.95}}}
    

    Expected output:

    [8.95, 12.99, 8.99, 22.99, 19.95]
    

    Actual output:

    [19.95, 8.95, 12.99, 8.99, 22.99]
    

For reference, the output was generated by the program in https://github.com/cburgmer/json-path-comparison/tree/master/implementations/Kotlin_com.nfeld.jsonpathlite.

Got null value when trying to get nested value inside array

Hello, given this JSON...

{
    "key": {
        "value": "root key value"
    },
    "array": [
        {
            "name": "name 0",
            "content": "content 0",
            "key": {
                "value": "key value 0"
            }
        },
        {
            "name": "name 1",
            "content": "content 1",
            "key": {
                "value": "key value 1"
            }
        }
    ]
}

And I read the "array[1]" with these codes

val value = JsonPath.parse(json)?.read<String>("$.array[1].key.value")

Got key value 1 as expected.

val values = JsonPath.parse(json)?.read<List<String>>("$.array[1].['name','content','key.value']")

Got [name 1, content 1] only not the 3rd one. Expected [name 1, content 1, key value 1] as a result from this code.

Please investigate. Thank you very much.

JSONPath comparison and standardisation

@cburgmer's JSONPath comparison project is currently discussing some issues relating to a proposed implementation of JSONPath known as "Proposal A". May I encourage you to get involved if you are interested in influencing the direction or, indeed, if you want to make your own proposals.

Similarly, please join us in slack (invitation) for informal discussions on the comparison project and potential JSONPath standardisation.

return JSON text instead of null

For example, I have such JSON

{
  "profile": {
    "name": "George",
    "address": {
      "home": "@ubuntu"
    }
  }
}

when I call JsonPath.parse(jsonText)?.read<String>("$.profile.address") it returns null. Instead return the object:

[
  {
    "home": "@ubuntu"
  }
]

My usecase is that there is a website im scraping, which returns huge JSON, but I dont want models for all the objects. If I could get those small snippets of JSON then I would be able to serialize them into smaller more useful models.

Maven definition

Hey,

I'm not really good at either Kotlin or Maven, so I've tripped over

<dependency>
  <groupId>com.nfeld.jsonpathlite</groupId>
  <artifactId>json-path-lite</artifactId>
  <version>1.1.0</version>
  <type>pom</type>
</dependency>

If I remove <type>pom</type>, then Maven will happily compile, otherwise it will fail with

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.3.40:compile (compile) on project query: Compilation failure
[ERROR] /Users/me/query/src/main/java/query/App.kt:[8,27] Unresolved reference: nfeld

Is there a reason for including it?

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.