Comments (3)
Please ask questions at the google group https://groups.google.com/forum/#!forum/jsonpath
Your JSON is not valid. I will answer based on the JSON below
[
{
"id": "1",
"name": "Test",
"location": {
"address": "Street 1",
"crossStreet": "Street 2"
}
},
{
"id": "1",
"name": "Test",
"location": {
"address": "Street 1"
}
}
]
The default configuration will return
List<String> crossStreets = JsonPath.parse(json).read("$[*].location.crossStreet");
Assertions.assertThat(crossStreets).containsExactly("Street 2");
If you want results from location
objects that does not define crossStreet
you must tweak the configuration
Configuration conf = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
List<String> crossStreets = JsonPath.using(conf)
.parse(json)
.read("$[*].location.crossStreet");
Assertions.assertThat(crossStreets).containsExactly("Street 2", null);
If this is your preferred default behaviour you can configure this when you bootstrap your application.
Configuration.setDefaults(new Configuration.Defaults() {
private final Set<Option> options = EnumSet.of(Option.DEFAULT_PATH_LEAF_TO_NULL);
@Override
public JsonProvider jsonProvider() {
return new JsonSmartJsonProvider(); //json-smart is not thread safe
}
@Override
public Set<Option> options() {
return options;
}
@Override
public MappingProvider mappingProvider() {
return new JsonSmartMappingProvider();
}
});
Hope this answers your question.
from jsonpath.
Hi @kallestenflo, thanks for reply.
I already have tried your suggestion and it's not working too.
I've posted a more completed question in GoogleGroups: https://groups.google.com/forum/#!topic/jsonpath/yPurMLMbB2Y
If you can see there I will be glad.
Thanks for your attention.
Regards.
from jsonpath.
For some reason, tweaking configuration on documentation (tweaking-configuration) does not work as expected, use this:
Configuration conf = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build();
from jsonpath.
Related Issues (20)
- Remove Analytics image from README
- Dot key + filter HOT 1
- Stack Overflow (Criteria.parse) HOT 19
- Default CacheProvider hides failing tests
- concurrency issue / race condition HOT 2
- Issue with Version 2.8 - JsonPath.java#set() and JsonPath.java#add() HOT 2
- get null when element not exists
- json-path-2.2.0.jar library vulnerability HOT 2
- will we see a YAML Path library? HOT 1
- The number read is counted by Scientific notation
- .set() method should add the key value if its not present HOT 1
- Support XPATH function substring-before,substring-after,translate
- Union of missing properties returns empty object
- Add support for custom Functions registration
- changelog.md is not updated HOT 1
- Provides transitive vulnerable dependency maven:com.jayway.jsonpath:json-path:2.8.0 CVE-2023-51074 5.3 Out-of-bounds Write vulnerability with Medium severity found Results powered by Checkmarx(c) HOT 2
- [2.8.0, 2.9.0] Aggregation function attempted to calculate value using empty array
- How to reference name of element itself when doing a filter
- Configuration method name does not match function
- Consider implementing RFC 9535 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonpath.