Giter Site home page Giter Site logo

Comments (3)

kallestenflo avatar kallestenflo commented on May 11, 2024 2

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.

avaz avatar avaz commented on May 11, 2024

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.

manusan avatar manusan commented on May 11, 2024

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)

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.