danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
697 stars 160 forks source link

JMESPath doesn't support JSON keys that are numeric #475

Closed lrosenthol closed 8 months ago

lrosenthol commented 8 months ago

Describe the bug

The JSON spec allows for keys which are numeric (strings). JMESPath supports these using quoted syntax on the key name.

Given, the following JSON: {"foo": {"1": ["one", "two", "three"], "-1": "bar"}} and the following JMESPath expression: foo."1" I would expect a result of: ["one", "two", "three"]

If I test this on the JMESPath tutorial (https://jmespath.org/tutorial.html#functions), I get the correct result.

However, using jsoncons, it returns null.

What compiler, architecture, and operating system?

I am trying this in XCode 14.3.1 on Mac OS 13.5.1 (Intel), but pretty sure that isn't the issue.

I am on the current/head from git of jsoncons.

danielaparker commented 8 months ago

I tried the following example in all environments that we test with, including Windows, Ubuntu, and macOS, Visual Studio, clang and g++,

std::string json_string = R"({"foo": {"1": ["one", "two", "three"], "-1": "bar"}})";
jsoncons::json doc = jsoncons::json::parse(json_string);

jsoncons::json result = jsoncons::jmespath::search(doc, R"(foo."1")");

std::cout << result << "\n";

In all cases I got

["one","two","three"]

Can you provide a complete example that produces your result?

lrosenthol commented 8 months ago

You are indeed correct! My JSON was incorrectly formatted :(. Once I fixed it, the expressions worked just fine.

Also tested the -1 and an index of the array. All good.

Closing. Thanks!