Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONQuery not returning the correct value #338

Closed
1 task done
Alexandre-Dhalenne opened this issue Oct 14, 2021 · 4 comments
Closed
1 task done

JSONQuery not returning the correct value #338

Alexandre-Dhalenne opened this issue Oct 14, 2021 · 4 comments

Comments

@Alexandre-Dhalenne
Copy link

Alexandre-Dhalenne commented Oct 14, 2021

Hello Daniel !

I just updated jsoncons to 0.168.0 from 0.159.0 and I am facing a bug.
It seems applying a JSONPath to my JSON is not giving same outputs than before.
Here is an example JSON :

{
      "version": "EC_v1",
      "data": {
        "paymentData": {
          "CCX": {
            "CCN": "3569990010095841",
            "CVV": "737",
            "DDMM": "0420"
          },
          "eciIndicator": "5"
        },
        "paymentDataList": [
          {
            "CCN": "3569990010095840",
            "CVV": "737",
            "DDMM": "0420"
          },
          {
            "CCN": "3569990010095841",
            "CVV": "737",
            "DDMM": "0421"
          },
          {
            "CCN": "3569990010095842",
            "CVV": "737",
            "DDMM": "0422"
          },
          {
            "CCN": "3569990010095843",
            "CVV": "737",
            "DDMM": "0423"
          },
          {
            "CCN": "3569990010095844",
            "CVV": "737",
            "DDMM": "0424"
          }
        ]
      }
    }

I want to apply the JSONPath $.data.paymentDataList[0,(@.length-1)].DDMM
Expected output :

[
  "$['data']['paymentDataList'][0]['DDMM']",
  "$['data']['paymentDataList'][4]['DDMM']"
]

Actual Output :

[
  "$['data']['paymentDataList'][0]['DDMM']",
  "$['data']['paymentDataList']['DDMM']"
]

cpp code used :

#include <jsoncons/json.hpp>
auto _jsonPath = "$.data.paymentDataList[0,(@.length-1)].DDMM";
jsoncons::ojson jsonObject = jsoncons::ojson::parse(exampleInput); // where exampleInput is a string containing the JSON above.
jsoncons::ojson result = jsoncons::jsonpath::json_query(jsonObject, _jsonPath, jsoncons::jsonpath::result_type::path);
std::vector<std::string> paths = result.as<std::vector<std::string>>();
for (const auto& path : paths) {
    std::cout<< path << std::endl;
}

Maybe I do something wrong but I checked release notes and this should be valid ?

Thanks a lot for your help.

Regards,

  • Compiler: g++
  • Architecture (e.g. x86, x64) x64
  • Operating system: linux

What jsoncons library version?

  • Latest release 0.168.0
@danielaparker
Copy link
Owner

You're right, it looks like we're not computing the path component for the expression part (@.length-1)] in the union. It works correctly for values, but not paths.

We'll fix that, but in the meantime, note that an equivalent (and I think preferred) query is

$.data.paymentDataList[0,-1].DDMM

which gives the expected results

["$['data']['paymentDataList'][0]['DDMM']","$['data']['paymentDataList'][4]['DDMM']"]

@Alexandre-Dhalenne
Copy link
Author

Indeed I spotted this error in my unit tests that aims at testing the union 😄

Thanks for your fast answer I will disable my unit test while waiting for a new release !

Have a good day,

@danielaparker
Copy link
Owner

It's fixed on master.

danielaparker added a commit that referenced this issue Oct 24, 2021
@danielaparker
Copy link
Owner

Fixed in 0.168.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants