json-path / jsonpath Goto Github PK
View Code? Open in Web Editor NEWJava JsonPath implementation
License: Apache License 2.0
Java JsonPath implementation
License: Apache License 2.0
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
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.
How about to create a JsonPath.tryRead
method that returns null
instead throwing PathNotFoundException
?
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.
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!
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:
Thanks and kind regards
Maria Beurer
am using
'net.minidev:json-smart:2.1.1'
'net.minidev:asm:1.0.2'
"org.slf4j:slf4j-api:1.7.10"
'com.jayway.jsonpath:json-path:2.0.0'
still getting the error java.lang.NoClassDefFoundError: org/jsonpath/JsonJar
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.
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?
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.
Hey,
I wonder if you are aware of JSR 353 API that is apparently part of JavaEE 7 specification.
Looking around in the source at least I could not see any obvious references to that.
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
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.
I have a fairly large json object that fails to return results. It passes http://jsonlint.com/
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"
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.
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
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.
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
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
Hi All,
Reading an object using JsonPath.read(String json, ....) does not maintain attribute order and is limited to 130 attributes.
Is there any way to handle this via configuration?
Thanks, Javier.
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})]
How to match "(left instanceof Number && right instanceof Number)" in safeCompare?
Thanks a lot.
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?
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.
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
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:
Thanks in advance!
Franck.
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
Multiple dex files define Lorg/objectweb/asm/AnnotationVisitor;
error in Android dex compilation phase. When using JSON-Path with Mockito.
The JSON-Smart dependency packages asm classes under default namespace.
See Issues:
Can the JSON-Smart dependency version be upgraded to latest release ?
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:
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.
how to get the Elements length?
$.col.length?
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!
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 .
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.
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)
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());
}
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!
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.
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.
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
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.
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.
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\"}";
}
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.
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);
}
};
}
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
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();
}
}
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
}
]
}
]
}
}
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"
]
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.