Giter Site home page Giter Site logo

jsonpath's People

Contributors

alanscut avatar arifogel avatar cindychow123 avatar davsclaus avatar dhalperi avatar gawrav avatar greek1979 avatar helsing avatar hezonghan avatar jacksingleton avatar jlolling avatar jmlamare avatar jochenberger avatar johanhaleby avatar kallestenflo avatar kysnm avatar marcp04 avatar maxxyme avatar mgreenwood1001 avatar oswaldobapvicjr avatar pigdrum avatar r0goyal avatar reftel avatar richardstartin avatar shoothzj avatar singingbush avatar trajano avatar valfirst avatar youngea avatar zline 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  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  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  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

jsonpath's Issues

update pom to point to json-smart 2.2 (to pull updated asm dependency)

Hello, the current pom has a depdency on json-smart 2.1.1, which in turn, has a direct dependency on {net.minidev, asm, 1.0.2} which in turn pulls in {asm, asm, 3.3.1}.

This unfortunately causes package namespace pollution as it has the package structure "org.objectweb.asm.*".

If you change the pom to have a dependency on json-smart-2.2, it correctly has a dependency on {net.minidev, accessors-smart} which correcstly pulls in the asm dependnecy by specifying org.object.web.asm

class not found exeption com/jayway/jsonpath/Predicate

Tried to use this library.
I'm using 2.0 with maven, dependency in pom.xml looks like

....
 <dependency>                              
     <groupId>com.jayway.jsonpath</groupId>
     <artifactId>json-path</artifactId>    
     <version>2.0.0</version>              
 </dependency>                             
....

Code

 22 public class App {                                                           
 23     public static class TokenizerMapper                                      
 24             extends Mapper<Object, Text, Text, IntWritable> {                
 25         private final static IntWritable one = new IntWritable(1);           
 26         private Text word = new Text();                                      
 27                                                                              
 28         public void map(Object key, Text value, Context context)             
 29                 throws IOException, InterruptedException {                   
 30             String text = JsonPath.read(value.toString(), "$.d3Params.cr");  
 31                                                                              
 32             word.set(text);                                                  
 33             context.write(word, one);                                        
 34         }                                                                    
 35     }                                                                        

And here is the exception

java.lang.Exception: java.lang.NoClassDefFoundError: com/jayway/jsonpath/Predicate
        at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
        at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.NoClassDefFoundError: com/jayway/jsonpath/Predicate
        at com.d3media.app.App$TokenizerMapper.map(App.java:30)
        at com.d3media.app.App$TokenizerMapper.map(App.java:23)
        at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:784)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
        at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.jayway.jsonpath.Predicate
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

Also, I'm new to java so maybe doing something silly.

read operations missing class parameter

Hi!

Similar to issue #72, the readme doc uses calls like:

Date date = JsonPath.parse(json).read("$['date_as_long']", Date.class);

Seems like those operations are not part of the released artifacts. Tested with release 1.2.0 and 2.0.0.

Greets
Stephan

How to get child of parent while filtering by neighbor of this child object ?

How can I get value parameter of an item in chilren list using the code parameter ?

I found only one way to do this and I dont like it:

$.children.[?(@.info.code == 'code1')]..value

I dont like that the filter expression does not iterate recursively using double dots like

$.children.[?(@..code == 'code1')]..value -- this sentence gives me an error.

Is there any way to get value using shorter expression and in more elegant way?
Because it's an example, on real world parameter names can be way longer. and the structure itself may be deeper so I need to write shorter queries.

{
    "object": {
        "children": [
            {
                "info": {
                    "id": "1",
                    "code": "code1"
                },
                "values": [
                    {
                        "value": "test"
                    }
                ]
            },
            {
                "info": {
                    "id": "2",
                    "code": "code2"
                },
                "values": [
                    {
                        "value": true
                    }
                ]
            }
        ]
    }
}

Bug in more complex filters?

Appears to be an issue with more complex filters. Would expect all test cases in below code to pass:

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class JaywayBug
{
    @Test
    public void testFilterWithOrShortCircuit1() throws Exception {
        final String myJson = "{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}";
        assertThat(SimpleJobFunctions.filter("$[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Doe')].*").call(myJson)).isFalse();
    }

    @Test
    public void testFilterWithOrShortCircuit2() throws Exception {
        final String myJson = "{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}";
        assertThat(SimpleJobFunctions.filter("$[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Smith')].*").call(myJson)).isTrue();
    }
}

Issue in management of alternates [q, r, ...]

Hi again,

I have a second question.
Here is JSON document:
{ "p":{
"s": { "u": "su" },
"t": { "u": "tu" }
}}

Expression $.p.['t'].u returns "tu" => ok.
Expression $.p.['s', 't'].u returns nothing, I'd expect "su" and "tu".

Another document : { "p": ["valp", "valq", "valr"] }
Expression $.p[?(@[0] == 'valp')] returns nothing, I would expect ["valp", "valq", "valr"]
Expression $.p[?(@.length == 3)] returns nothing, I would expect ["valp", "valq", "valr"]

Am I mistaken or is there actually a bug somewhere?

Thx,
Franck.

Comma in selectors

It seems that JsonPath doesnโ€™t support commas in queries such as the following:

$.phoneNumbers[?(@.type=='iPhone','home')].number

And we need to use the more verbose:

$.phoneNumbers[?(@.type=='iPhone' || @.type=='home')].number

Iโ€™m using the example data from http://jsonpath.com/ where you can check that both work.

Just a minor inconvenience that I noticed. Thaks for the good work!

Regression from 0.9.1

There is a regression in 1.2.0 from 0.9.1, where a complex input document and expression simply doesn't agree correctly.

The code is found below, and the exception shows that it misunderstands the Array situation of the "[0]" in "path2", although it clearly extracts a JSONArray in if I remove the "[0]" as seen in the code below.

Exception in thread "main" com.jayway.jsonpath.InvalidPathException: Filter: [0] can only be applied to arrays. Current context is: 51.968503937007874
at com.jayway.jsonpath.internal.token.ArrayPathToken.evaluate(ArrayPathToken.java:60)
at com.jayway.jsonpath.internal.token.PathToken.handleObjectProperty(PathToken.java:71)
at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:44)
at com.jayway.jsonpath.internal.token.PathToken.handleArrayIndex(PathToken.java:125)
at com.jayway.jsonpath.internal.token.PredicatePathToken.evaluate(PredicatePathToken.java:67)

import com.jayway.jsonpath.JsonPath;

public class JsonPathFailureTest
{
    public static void main( String[] args )
    {
        JsonPath path1 = JsonPath.compile( "$.blocks[?(@.name=='AQ')].edges[?(@.name=='value')].value" );
        Object result1 = path1.read( JSON );
        System.out.println( result1 );
        System.out.println( result1.getClass() );

        JsonPath path2 = JsonPath.compile( "$.blocks[?(@.name=='AQ')].edges[?(@.name=='value')].value[0]" );
        System.out.println( path2.read( JSON ) );
    }

    private static final String JSON =
        "{" +
        "    \"blocks\":[{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/aq\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"-1.0\",\"tracked\":true}],\"name\":\"regMode\",\"type\":\"AnalogOutput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/dq\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false}],\"name\":\"DQ\",\"type\":\"AnalogOutput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/i\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"50.0\",\"tracked\":false}],\"name\":\"I\",\"type\":\"AnalogOutput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/setp\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"55.0\",\"tracked\":false}],\"name\":\"BV\",\"type\":\"AnalogOutput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"out\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"55.5\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/ai\",\"tracked\":false}],\"name\":\"GT1\",\"type\":\"AnalogInput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/p\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"2.0\",\"tracked\":false}],\"name\":\"P\",\"type\":\"AnalogOutput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"out\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"51.968503937007874\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/aq\",\"tracked\":false}],\"name\":\"AQ\",\"type\":\"AnalogInput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"out\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"55.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/setp\",\"tracked\":false}],\"name\":\"actualSetp\",\"type\":\"AnalogInput\"},{\"edges\":[{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"expr\",\"type\":\"String\",\"value\":\"\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"k\",\"type\":\"Double\",\"value\":\"1.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"m\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"out\",\"low\":null,\"name\":\"value\",\"type\":\"Double\",\"value\":\"0.0\",\"tracked\":false},{\"high\":null,\"io\":\"in\",\"low\":null,\"name\":\"url\",\"type\":\"String\",\"value\":\"xnet://81/dq\",\"tracked\":false}],\"name\":\"actualDQ\",\"type\":\"AnalogInput\"}],\"connections\":[{\"from\":\"VVC.choice.output\",\"to\":\"SG-VV.DQ.value\",\"type\":\"Double\"}],\"name\":\"SG-VV\"}";

}

JsonPath.read() ignores REQUIRE_PROPERTIES config option when AS_PATH_LIST is set

Description
When I specify this (Note: REQUIRE_PROPERTIES is NOT set):
Configuration jsonCfg = Configuration.builder().options( Option.AS_PATH_LIST ).build();

And do this:

String jsonStr = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }";
Predicate[] predicates = {};
DocumentContext dCtx = JsonPath.using( jsonCfg ).parse( jsonStr );
readPaths = dCtx.read( JsonPath.compile( "$.*.bar", predicates ) );

Actual Behaviour
I get this:

Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: No results for path: $['moo']['bar']
    at com.jayway.jsonpath.internal.token.PathToken.handleObjectProperty(PathToken.java:53)
    at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:44)
    at com.jayway.jsonpath.internal.token.PathToken.handleObjectProperty(PathToken.java:71)
    at com.jayway.jsonpath.internal.token.WildcardPathToken.evaluate(WildcardPathToken.java:32)
    at com.jayway.jsonpath.internal.token.RootPathToken.evaluate(RootPathToken.java:53)
    at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:53)
    at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:61)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:176)
    at com.jayway.jsonpath.internal.JsonReader.read(JsonReader.java:146)

Expected Behaviour
I expected a List result containing a single path: $[foo].[bar] because I did not have the REQUIRE_PROPERTIES option set. I would only expect to see PathNotFoundException if REQUIRE_PROPERTIES was set.

Creation of deep nodes

It would be useful if JsonPath had the capability to create "deep nodes" rather than just nodes of already existing parents.

For example, provide a method that, given a path e.g. $.x.y.z, a value 10, and a DocumentContext, create the node z with a value 10 allowing for the possibility that x, y do not yet exist. The logic would be

  • If the 1st level node x does not yet exist, create it with an empty object value
  • If the 2nd level node x.y does not yet exist, create it with an empty object value (i.e. create a new property y of x)
  • If the 3rd level node x.y.z does not yet exist, create it with value 10; if it does exist update the value to 10.

It's possible to do this with the existing APIs using an iterative loop to work down the path, DocumentContext.read to test for existence and DocumentContext.put to create, but would be really cool if the API provided such a capability generally.

Missing PathNotFoundException in nested arrays.

Hey,
I got the following json

{
  "array1": [
    {
      "array2": []
    },
    {
      "array2": [
        {
          "key": "test_key",
        }
      ]
    }
  ]
}

And i want to read the following path : $.array1[*].array2[0].key.
Actually my goal was to retrieve a list with [null, "test_key"] but this didnt work, however I expected at least a PathNotFoundException for $.array1[0].array2[0].key. Instead I get a list with ["test_key"].

greetings,
jan

Upgrade JSON-Smart dependency

Issue

Multiple dex files define Lorg/objectweb/asm/AnnotationVisitor; error in Android dex compilation phase. When using JSON-Path with Mockito.

Probable cause

The JSON-Smart dependency packages asm classes under default namespace.
See Issues:

Can the JSON-Smart dependency version be upgraded to latest release ?

Failed to parse "Infinity" and "-Infinity" to `Double.POSITIVE_INFINITY` and `Double.NEGATIVE_INFINITY`

Hi, Iโ€™ve just came across this problem with v2.0.0. When parsing a JSON array of doubles such as the following:

{foldChanges: [
    { foldChange: "Infinity"},
    { foldChange: "-Infinity"},
    { foldChange: 3.54051821666666},
    { foldChange: -3.53462115}
]}

The following fails to create a list of Double, the two first elements are always a String:

List<Double> foldChanges = jsonReadContext.read("$.foldChanges[*].foldChange");

No matter the type of List that I choose, the first two values are always a String and the last two are Double. I would hope that an option could be included to make force some sort of type coercion. If somebody wants to parse the previous JSON you need to check if the read value is either a String or a Double first.

SUPRESS_EXCEPTIONS not working when document is parsed separately?

Depending on which approach I take to parse a json string with JsonPath (either parse the string every time for each path, or only once before paths are read), the SUPRESS_EXCEPTIONS option behaves differently. Specifically, it does not seem to work as expected if I use the "parse only once" approach. Here are my two attempts:
(note: json is a json String representing an object with no 'comment' field value)
("parse only once" approach):
1- Configuration conf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();
Object document = conf.jsonProvider().parse(json);
String commentBody = JsonPath.read(document, "$.comment.body");

Produces: PathNotFoundException: Property ['body'] not found in path $['comment']
I would have expected no exception would be produced, which is what happens below:

("parse for every path approach")
2- Configuration conf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();
String commentBody = JsonPath.using(conf).parse(json).read("$.comment.body");

This code does not produce any exceptions

Is this a bug or am I misunderstanding how SUPRESS_EXCEPTIONS is supposed to work?

Matching for JSON objects/arrays not just strings

I have a patch that allows you to match JSON fields to arrays or objects. Before I bother my company about approving a patch, I'd like to know if that is something you'd accept.

E.g. you can do this:

[?(@.foo == ["A", "B"])]
[?(@.foo == {"A": true})]

read methods with JsonPath parameter missing

Hi!

First of all thanks for the great library! I've noticed that there seem to be some features missing from the release compared to the documentation in the readme. According to the readme doc:

*All read operations are overloaded and also supports compiled JsonPath objects. This can be useful from a performance perspective if the same path is to be executed many times.

`JsonPath compiledPath = JsonPath.compile("$.store.book[1].author");

String author2 = JsonPath.read(document, compiledPath);`*

I've worked with release 1.2.0 and now 2.0.0 and those operations do not exists. Am I missing something? Do I need additional libraries? Otherwise I'd suggest to remove this part from the docs or mark as not yet supported.

Thanks Stephan

Can't be used in hive-0.11: java.lang.NoClassDefFoundError

The pom.xml was set according to your DOC: Getting Started.

I run it in my laptop, it works well. However, when run it on hive 0.11, error occurred:

Caused by: java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
at com.jayway.jsonpath.internal.DefaultsImpl.(DefaultsImpl.java:17)
at com.jayway.jsonpath.internal.DefaultsImpl.(DefaultsImpl.java:15)
at com.jayway.jsonpath.Configuration.getEffectiveDefaults(Configuration.java:53)
at com.jayway.jsonpath.Configuration.defaultConfiguration(Configuration.java:178)
at com.jayway.jsonpath.internal.JsonReader.(JsonReader.java:48)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:461)
at com.qunar.hive.udf.ParseJsonWithPath.evaluate(ParseJsonWithPath.java:64)
... 26 more
Caused by: java.lang.ClassNotFoundException: net.minidev.json.writer.JsonReaderI
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 33 more

Could anybody help me? Thanks a lot.

Feature Request: Make dependencies on the different providers json-smart, jackson, gson optional

The current 2.0.0 version has in the OSGi manifest mandatory imports for the different JSON providers:
com.fasterxml.jackson.core;version="[2.4,3)",
com.fasterxml.jackson.databind;version="[2.4,3)",
com.fasterxml.jackson.databind.type;version= "[2.4,3)",
com.google.gson;version="[2.3,3)",
com.google.gson.internal; version="[2.3,3)",
com.google.gson.reflect;version="[2.3,3)",
net.minidev.json;version="[2.1,3)",
net.minidev.json.parser;version="[2.1,3)",
net.minidev.json.writer;version="[2.1,3)"

It would be great to make all these dependencies optional, so that the OSGi bundle gets resolved without all the JSON providers. Of course you have to provide at least one provider to be able to run jasonpath successfully. However, this solution would be much leaner.

Beside the changes in the manifest file, also changes in the DefaultsImpl must be made. This class must be made more flexible: If the json-smart provider is not in the classpath, this class could look for providers which are in the classpath and use the first found provider.

If you which I can create a pull request.

Incorrect comparison

I have the following json,

{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22}],"bicycle":{"color":"red","price":19}},"ExDataList":[{"key":"numOfPages","value":"2341","timestamp":"08-13-2015 06:32:13"},{"key":"numOfImages","value":"69","timestamp":"08-13-2015 06:32:13"},{"key":"numOfComments","value":"89","timestamp":"08-13-2015 06:32:13"},{"key":"numOfBookmarks","value":"3","timestamp":"08-13-2015 06:32:13"}],"expensive":10}

On the above json, i am trying to evaluate the following jsonpath expression,

$.ExDataList[?(@.key=='numOfImages' && @.value>100)]

Ideally the above expression should return an empty arraylist, however it is returning the following,

[{"key":"numOfImages","value":"69","timestamp":"08-13-2015 06:32:13"}]

But if, i replace the "100" in the jsonexpression with any other value, say 98, then it is returning the correct output.

I am using the latest release of Jayway JsonPath .

Indexing into null return null option

Hi I am using jsonPath and hit an edge case in which i can't extract things in a persisting order:

{
"context" : [
{ "level1" : { "value1": 1, "value2": { "inner": 2 } } },
{ "level1" : { "value1": 3 } },
{ "level1" : { "value1": 4, "value2": { "inner": 5 } } }
]
}

"$.context.level1.[*].value2" with DEFAULT_PATH_LEAF_TO_NULL will return
[
{ "inner": 2 },
null,
{ "inner": 5}
]

ideally i want to extract [ 2, null, 5 ], and there's no way to get this now because
"$.context.level1.[ * ].value2.[ * ].inner" will return [ 2, 5 ]
*note that if "value2" for "level1" is {} instead this will [ 2, null, 5 ], but it's not possible for me to get that for now

I am wondering if it's possible/reasonable to support indexing into null return null as a configuration option?

Thanks!

How to obtain a list of all xpaths from a json document

Sorry to create an Issue, but I could not find a Forum.

I wish to parse Json documents and turn them into tables for use in Fitnesse tests. To do this I considered obtaining the list of all definite (canonical?) json-path expressions, which would form the left-hand column of the table, and then evaluating each path expression in turn to fill in the right-hand column.

In Fitnesse I could then constrain that a specific json-path matched the expected node value. I could also see what "supurfluous" node values existed in the document (otherwise I would just invoke individual xpath expressions in turn without ever having to know the full set of path expressions availabls in a given document.

So, any hints as to how to establish the full canonical set of json-path expressions realized in a given json document?

Thanks, Robin.

Odd path read error

I have a very simple project that I generated with mvn:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app

I then updated with a simple pom.xml and updated the AppTest.java with:

    try {

            JsonPath path2 = JsonPath.compile( "$.userName" );

            ReadContext ctx = JsonPath.parse("{\"userName\":\"tstark\"}");

            path2.read(ctx);

            assertTrue(true);

            System.out.println("SUCCESS");

    } catch(Exception e){

            e.printStackTrace();

            assertTrue(false);

    }

    assertTrue( true );

I would expect this to print "SUCCESS" but instead I get a:
com.jayway.jsonpath.PathNotFoundException: Property ['userName'] not found in path $

This is being tested on:
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

Reaching path after filtering

I have the same issue as posted in this question. The path is as follows:

$.store.book[?(@.category==fiction)].price

Filtering returns LinkedHashMap, but .price returns JSONArray. I tried .price[0] but InvalidPathException is thrown then. What is the solution to this problem?

Make use of org.json

Hi,
I just strumbled upon this library so don't blame me if I didn't see anything obvious.
I'm just wondering why the library has no interface to the popular classes of org.json.*.

Have a nice day
Hubert Grzeskowiak

Referring to closed issue 113

Hi guys, sorry to open a new issue but that one #113 is closed and I did not know how to make sure someone was gonna read my last comment ;-)

So if one of you, or kallestenflo how answered, has any time, would be great if you can have a look.

Thx,
Franck.

IllegalArgumentException even when Config.SUPPRESS_EXCEPTIONS is set

            Configuration config = Configuration.defaultConfiguration()
                    .addOptions(Option.SUPPRESS_EXCEPTIONS)
                    .addOptions(Option.ALWAYS_RETURN_LIST)
                    .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
            DocumentContext parsedJson = JsonPath.using(config).parse("");

The above code will throw an IllegalArgumentException, because the string value being passed to parse is empty. It was my expectation that SUPRESS_EXCEPTIONS option would supress all json parsing exceptions. This is helpful when guarding an invalid input to parse(), and the IllegalArgumentException should not be thrown if SUPRESS_EXCEPTIONS config option is set.

What kind of expression is allowed in script expressions?

Hi guys,

First of all, thx for sharing this project that I found very useful.

I have a question regarding script expressions. I'm referring to Stefan Goessner's page:

  • ?() : applies a filter (script) expression => what kind of script is it, JavaScript where any occurrence of "this" would be replaced with "@"? Is any JavaScript expression allowed here or is there any restriction?
  • () : script expression, using the underlying script engine => what is the underlying script engine that you have included in your implementation? Same question: if this is JavaScript where any occurrence of "this" would be replaced with "@"? Is any JavaScript expression allowed here? Any restriction?

Thanks in advance!
Franck.

How to deal with Optional Paths answer

Hi,

Sorry if this is answered is somewhere else but I can't find the solution.
I'm consuming a service that sends back a json with fields that are optional, they can be returned or not with regarding of the query that I send to it. How I deal with this situation?
Example:

[
{
"id": "1",
"name": "Test",
"location": {"address": "Street 1", "crossStreet": "Street 2"}
},
{
"id": "1",
"name": "Test",
"location": {"address": "Street 1"}
}
] 

The crossStreet field is optional, how I define optional fields in jsonpath? Because, on my code, when I use $.location.crossStreet I get an com.jayway.jsonpath.PathNotFoundException: No results for path: $['location']['crossStreet'] exception. How can I avoid the exception?

Thanks.

How to traverse array which is the root element

I'm struggling with writing correct path to extract balanceName from:

[
  {
    "balanceName": "PROJECTED"
  },
  {
    "balanceName": "FAILED"
  }
]

In online parser based on Javascript I found couple working versions but I haven't succeeded with JsonPath

Checking if Node Exists - Bracket Notation Syntax

Hi,

I currently have data file with rows of json objects containing information I want to extract.

Currently, there is a field called random which will sometimes appear in some of the data.

[1, 2, {"d": { "random":null, date: 1234} , "l": filler } ]

Using bracket notation, to check if a node exists, should the syntax not be:

$[2]['d'][?(@.random)]['date']

The filter checks if the node exists, and if it does, then the query will retrieve date. Is this the proper way to do it, or is there an underlying issue with bracket notation with jsonpaths.

Thank you

Configuration builder forces dependency on Minidev json-smart

In the configuration builder, it always calls getEffectiveDefaults, even when it is unnecessary. If you have not set the defaults, but have set both jsonProvider and mappingProvider, the call to getEffectiveDefaults is unnecessary, and only serves to introduce a dependency.

This is a problem when you are another library. You do not want to override the defaults, because the users of your library might be depending on those defaults. However, you have your own providers set, such that the defaults will not be used, ensuring safe operation.

IndexOutOfBoundsException in DocumentContext.delete

Hi,

I am trying to remove some elements from an array. It usually works well, but there are times it throws an IndexOutOfBoundsException. Here is an example:

    @Test
    public void testName() throws Exception {
        String json = "{ \"books\": [ " +
                "{ \"category\": \"fiction\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"fiction\" }, " +
                "{ \"category\": \"fiction\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"fiction\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"reference\" }, " +
                "{ \"category\": \"reference\" } ]  }";
        Configuration conf = Configuration.builder()
                .jsonProvider(new GsonJsonProvider()).mappingProvider(new GsonMappingProvider()).build();
        DocumentContext context = JsonPath.using(conf).parse(json);
        context.delete("$.books[?(@.category == 'reference')]");
    }
java.lang.IndexOutOfBoundsException: Index: 10, Size: 6
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.remove(ArrayList.java:492)
    at com.google.gson.JsonArray.remove(JsonArray.java:106)
    at com.jayway.jsonpath.spi.json.GsonJsonProvider.removeProperty(GsonJsonProvider.java:184)
    at com.jayway.jsonpath.internal.PathRef$ArrayIndexPathRef.delete(PathRef.java:121)
    at com.jayway.jsonpath.JsonPath.delete(JsonPath.java:234)
    at com.jayway.jsonpath.internal.JsonReader.delete(JsonReader.java:204)
    at com.jayway.jsonpath.internal.JsonReader.delete(JsonReader.java:199)
[...]

It seems to me that JsonPath.delete(Object, Configuration) either is not updating the calculated indexes as it removes elements or it is not ordering the indexes from high to low.

Please let me know if you need more information.

Thank you.

FIilter "toString()" and consequent "parse" fail

Hi.

In order to Serialize a Filter object I tried to send it as the string result of Filter's toString() method.

I don't know how can I parse the string obtained as before in order to get the original Filter obj.

Is there a way to "Serialize" a filter obj?

Thanks.

ECCN for jayway JSON path

Hello,
We use jayway JSON path in our commercial product and therefore need to know the ECCN for it.
The ECCN is an international Export control and compliance number that is necessary for EVERY Export. Most of the european countries specify their goods according to the european commerce control list (EG dual-use list : http://ec.europa.eu/trade/import-and-ex ... -controls/).
So for export of a commercial Software using jayway JSON path ( or any other open source or OEM Software), we need to document the libs, on one side to comply with license obligations and on the other side the ECCNs for Export restrictions. Please note, as the US regulations also care about re-export of US items ( any stuff that contains US Content above a certain Limit), we need to specify both; the EU-ECCN and the US-ECCN and hopefully the US license exception that applies (e.g. TSU for open source).

So please specify the ECCN for jayway JSON path or answer the following questions:

  • Country of origin ( where is jayway JSON path Software mainly developed)
  • is there US Content in there? ( e.g. other open source libs)
  • Is the SW designed or modified to use cryptography performing any cryptographic function other than authentication or digital signature? (y/n)
    โ€“if yes,
    -- key length symetric
    -- Bit length asymmetric

Thanks and kind regards
Maria Beurer

Incorrect error message for JsonPath.read(Object)

I am using JsonPath version 2.0.0.
I was trying to do this:

com.google.gson.JsonObject json = ...; // { "foo": "bar" }
JsonPath path = JsonPath.compile("$.foo");
String object = path.read(json);

Actual behavior:

    [junit] com.jayway.jsonpath.PathNotFoundException: Property ['foo'] not found in path $
    [junit]     at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:41)
    [junit]     at com.jayway.jsonpath.internal.token.RootPathToken.evaluate(RootPathToken.java:53)
    [junit]     at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:53)
    [junit]     at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:61)
    [junit]     at com.jayway.jsonpath.JsonPath.read(JsonPath.java:178)
    [junit]     at com.jayway.jsonpath.JsonPath.read(JsonPath.java:155)

Expected behavior:
One of the following:

  1. Throw ClassCastException
  2. Error message that says: the document must be identified as either a List or Map by the JsonProvider.
  3. call toString() on the object and try to parse the resulting String.

I find the current behavior to be very misleading: it makes it sound as though the problem is in the parameter to JsonPath.compile, when in fact the problem is in JsonPath.read.

Add hamcrest-matchers based on JsonPath expressions

Are there any others that would love to have hamcrest-matchers based on JsonPath?

I would like to express my tests like this:

assertThat(json, hasJsonPath("$.store.book[*].author", equalTo("Nigel"));

Currently I need to implement something similar to below in my projects, but I would prefer if it was already provided in the json-path library in some different flavors depending on usage.

public static <T> Matcher<String> hasJsonPath(final String path, Matcher<? super T> matcher) {
    return new TypeSafeMatcher<String>() {
        @Override
        protected boolean matchesSafely(String json) {
            return matcher.matches(com.jayway.jsonpath.JsonPath.read(json, path));
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("has json path ").appendValue(path).appendText(" ").appendDescriptionOf(matcher);
        }
    };
}

Function "JsonPath.getList()" returns null when key contains a hyphen "-"

Hello,

I am pulling in com.jayway.restassured:json-path:2.6.0 as a dependency of com.jayway.restassured:rest-assured:2.6.0 (which is fantastic by the way!). When attempting to validate some properties of a JSON API, I ran into the following case, which looks like a bug.

Given a JSON document like {"key-name":["value1", "value2"]}, I can call JsonPath.getString("\"key-name\"[0]") to get the first list value value1.

But, if instead I ask for the entire list with JsonPath.getList("\"key-name\""), I just get null instead.

I have validated my syntax using other JSONPath parsers (like https://jsonpath.curiousconcept.com/) and everything looks correct. I have also included sample code below that shows how my syntax works for key with underscores, but fails for keys with hyphens.

import com.google.common.collect.ImmutableList;
import com.jayway.restassured.path.json.JsonPath;
import org.junit.Assert;
import org.junit.Test;

public final class JsonTest {

    @Test
    public void testParse() {
        String body;
        JsonPath json;

        // Original JSON, notice that the key contains an underscore
        body = "{\"key_name\":[\"value1\", \"value2\"]}";
        json = new JsonPath(body);

        // Assert #1 passes
        Assert.assertEquals(
            "value1",
            json.getString("key_name[0]")
        );

        // Assert #2 passes
        Assert.assertEquals(
            "value1",
            json.getString("\"key_name\"[0]")
        );

        // Assert #3 passes
        Assert.assertEquals(
            ImmutableList.of("value1", "value2"),
            json.getList("\"key_name\"")
        );

        // Same as original JSON, notice that the key contains a hyphen instead of an underscore
        body = "{\"key-name\":[\"value1\", \"value2\"]}";
        json = new JsonPath(body);

        // Assert #4 passes
        Assert.assertEquals(
            "value1",
            json.getString("\"key-name\"[0]")
        );

        // Assert #5 fails
        //java.lang.AssertionError: expected:<[value1, value2]> but was:<null>
        Assert.assertEquals(
            ImmutableList.of("value1", "value2"),
            json.getList("\"key-name\"")
        );
    }

}

If there is a workaround that allows me to extract the desired values, that would also be great, as it's completely possible that I'm not escaping something properly.

Thank you!

memory leak

One of our QA servers ran out of memory, looks like there is a memory leak in Cache.java.
A simple single-threaded program to reproduce the issue is below.
I think it is better to allow the user to disable the cache, and let the user create (and cache) CompiledPath instances.

public static void main(String[] arg) {
    Cache cache = new Cache(200);
    Path dummy = new CompiledPath(null, false);
    for (int i = 0; i < 10000; ++i) {
       String key = String.valueOf(i);
       cache.get(key);
       cache.put(key, dummy);
    }
    System.out.println("cache size: " + cache.size());
}

Index out of bounds

Given JSON:

{ 
  "c": {
    "d1": {
      "url": [ "url1", "url2" ]
    },
    "d2": {
      "url": [ "url3", "url4","url5" ]
    }
  }
}

The path:

$.c.*.url[2]

throws an exception

Index out of bounds when evaluating path $['c']['d1']['url'][2]

Expected result:

[
   "url5"
]

queries to large json objects fail to return a result.

I have a fairly large json object that fails to return results. It passes http://jsonlint.com/

http://pastebin.com/BBb2B2BY

Running even simple queries has failed to return consistent results in both my code and https://jsonpath.herokuapp.com.

For example:
$.frameworks returns nothing
$.frameworks[?(@.id == '20150127-110351-1513192458-5050-8587-0000')] returns the entire object in java and nothing online.
$.frameworks[?(@.hostname =~ /.*cloud.in.here.com/)] returns the correct result in java and nothing online.

I am using JsonPath 2.0.0 and java version "1.7.0_67"

Member names containing dot fail

when member names containing dot , jsonpath not work properly, for example:
{"resultMap":{
"accessPoint.uri":"https://xxx
"accessPoint.password":"yyy",
"accessPoint.username":"zzz",
"accessPoint.category.name":"URL"
}}

use jsonpath :$.resultMap.'accessPoint.uri' can't get result.

Json path query is not working as expected

JSON :
{
"key" : "value",
"array" :
{ "key" : 1 },
{ "key" : 2, "dictionary": {
"a": "Apple",
"b": "Butterfly",
"c": "Cat",
"d": "Dog"
} },
{ "key" : 3 }
]
}

Extraction Query:
$.array[?(@.key=2)].dictionary.b

Result:
[
"Butterfly"
]

Im trying using your library giving an output of empty list

validationObj.jsonPath ="$.array[?(@.key=2)].dictionary.b"

JsonPath.read(responseJSONData, validationObj.jsonPath) -->[ ] instead of ["Butterfly"]

Please your help is appreciated

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.