-2

I have a JSON response where one of the keys value is holding "-", "," and space inside it. I was able to parse it correctly but now it does not seem to be working.

For replication, we can use http://jsonpath.com/

JSON:

{
  "data":{
        "ID":"123",
        "paraValues":{
            "DE:is this A": "Yes",
             "DE:is this B": "No",
             "DE:Is project part of a multi-piece initiative - campaign, event?" : "No"
        }
    }
}

I am trying to fetch the value of this key : "DE:Is project part of a multi-piece initiative - campaign, event"

I am using:

$.data.paraValues.DE:Is project part of a multi-piece initiative - campaign, event?

It didn't work.

Then I used after reading some answers:

$.data.paraValues.["DE:Is project part of a multi-piece initiative - campaign, event?"]

This also didn't work.

Update: Those who are coming here from java, issue can be resolved by upgrading the jsonpath library.

I added below library and it resolved the issue :

<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.7.0</version>
</dependency>

Note: in http://jsonpath.com/, accessing it is still failing.

QualityMatters
  • 895
  • 11
  • 31
  • What does *This also didn't work.* mean? `data.paraValues['DE:Is project part of a multi-piece initiative - campaign, event?']` provides `No` as it should. Also skip the `.` before brackets. – Lain Mar 31 '22 at 09:17
  • @Lain — they had a `.` before the `[` – Quentin Mar 31 '22 at 09:17
  • @lain Can you check your code in jsonpath.com and let me know if it works there. Its not working there. – QualityMatters Mar 31 '22 at 09:21
  • 1
    You tagged this [tag:javascript]. JSON Path expressions are completely different. – Quentin Mar 31 '22 at 09:22
  • 1
    It looks to doesnt like the `,` char in path. Just dont use it as key. It s weird because it looks to handle every special chars but not `,` – Clem Mar 31 '22 at 09:27
  • @Clem: Probably because `,` inside strings is the `Union operator`. You can escape it tho, [depending on the implementation](https://github.com/json-path/JsonPath/issues/400). – Lain Mar 31 '22 at 09:34
  • @Lain it s not escapable ? Thoses keys are weird anyway. – Clem Mar 31 '22 at 09:35
  • @Clem, no idea since I never used `JSONPath` before. I would fix the structure/key, as you said as well instead of looking for *workarounds* for an obvious flaw. – Lain Mar 31 '22 at 09:40
  • 1
    @QualityMatters Please use https://jsonpath.herokuapp.com/ for Jayway JSONPath. http://jsonpath.com/ is built using [jsonpath plus](https://www.npmjs.com/package/jsonpath-plus) – Akshay G Mar 31 '22 at 11:46

1 Answers1

0

This appears to be a bug in jsonpath-plus which is used by jsonpath.com.

If I switch out to the jsonpath module, then it works (if you remove the extra .).

const data = JSON.parse(`{
  "data":{
        "ID":"123",
        "paraValues":{
            "DE:is this A": "Yes",
             "DE:is this B": "No",
             "DE:Is project part of a multi-piece initiative - campaign, event?" : "No"
        }
    }
}`);
const result = jsonpath.query(
  data,
  '$.data.paraValues["DE:Is project part of a multi-piece initiative - campaign, event?"]'
);
console.log(result);
<script src="https://unpkg.com/jsonpath@1.1.1/jsonpath.js"></script>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks for your analysis. With dot and without dot does not make any effect if we reach other values in this json. For example: $.data.paraValues[DE:is this A] and $.data.paraValues.[DE:is this A] both returns same value. So the issue is with parsing the key having hyphen or spaces, which I asked in the question – QualityMatters Mar 31 '22 at 10:08
  • @QualityMatters — As I said, the website you are using appears to have a bug. It doesn't seem to have any way to support commas in key names. Don't use that website. Use a jsonpath engine which doesn't have that bug. – Quentin Mar 31 '22 at 10:09
  • Actually, They have a java library and we are extensively using it and I am getting issue there. To make it replicable, I gave that url, so people can easily replicate it. – QualityMatters Mar 31 '22 at 10:11
  • 1
    Then that library, presumably, has a similar bug. You've got a weird edge case where you are using a character which often has special meaning in jsonpath expressions and almost never appears in property names. It's not surprising that lots of parsers would be buggy about it. – Quentin Mar 31 '22 at 10:13
  • Agree with you. I am going to log a bug in their site for this issue. Thanks a lot for your analysis. – QualityMatters Mar 31 '22 at 10:15