json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.85k stars 1.63k forks source link

Get definite paths #475

Open ghost opened 6 years ago

ghost commented 6 years ago

Hello team,

I have a requirement to alter the values of certain elements in the Json document based on an indefinite path. For example: Given: $.a.b[*].c I need to change all of the c values based on the original c value i.e. cValue = cValue + 1 I know that I can update all the c values to the same value using document.set(), but I don't know how to get individual paths so that I can set to different values.

Please advise how to get all the definite paths based on the original indefinite paths, thank you very much!

Mandy

shumsky commented 6 years ago

Hi,

If I understood you correctly, you can achieve that by doing this:

Configuration conf = Configuration.builder()
   .options(Option.AS_PATH_LIST).build();

List<String> pathList = using(conf).parse(json).read("$..author");

assertThat(pathList).containsExactly(
    "$['store']['book'][0]['author']",
    "$['store']['book'][1]['author']",
    "$['store']['book'][2]['author']",
    "$['store']['book'][3]['author']");

as described in this section.

sheetaldolas commented 6 years ago

@shumsky , Thanks, that helps in some of my use cases. However, there are cases where it does not work as desired. For example.

Input Json

{
  "interpreterSettings": {
    "Complete": {
      "id": "Complete",
      "name": "phoenix",
      "group": "phoenix",
      "properties": {
        "phoenix.user": "",
        "useless.property": "p@ssw0rd",
        "phoenix.password": "p@ssw0rd",
        "default.password": "p@ssw0rd",
        "spark2.password": "p@ssw0rd",
        "spark.password": "p@ssw0rd",
        "psql.password": "p@ssw0rd",
        "hive_interactive.password": "p@ssw0rd"
      }
},
    "Incomplete": {
      "id": "Incomplete",
      "name": "phoenix",
      "group": "phoenix",
      "properties": {
        "hive.password": "p@ssw0rd",
        "incomplete.key": " incompleteValue"
      }
},
    "other": {
      "id": "other",
      "name": "phoenix",
      "properties": {
        "useless.property": "uselessValue",
         "hive.password": "p@ssw0rd",
        "phoenix.password": "p@ssw0rd",
        "random.property": "uselessValue",
        "spark2.password": "p@ssw0rd",
        "psql.password": "p@ssw0rd",
        "hive_interactive.password": "p@ssw0rd"
      }
    }
  }
}

Path being read $.interpreterSettings..properties.['hive.password','useless.property']

Desired output

[
   "$['interpreterSettings']['Complete']['properties']['useless.property']",
   "$['interpreterSettings']['Incomplete']['properties']['hive.password']",
   "$['interpreterSettings']['other']['properties']['hive.password']",
   "$['interpreterSettings']['other']['properties']['useless.property']"
]

However actual output is

[
   "$['interpreterSettings']['Complete']['properties']['hive.password', 'useless.property']",
   "$['interpreterSettings']['Incomplete']['properties']['hive.password', 'useless.property']",
   "$['interpreterSettings']['other']['properties']['hive.password', 'useless.property']"
]