Behavior differences in operator[] object creation with a json_pointer
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 50.6k
- Forks
- 7.5k
- Avg merge
- 4d 17h
- Merged PRs (30d)
- 58
Description
Discussed in https://github.com/nlohmann/json/discussions/5356
Originally posted by samcmill August 3, 2026
The following code throws a parse_error.106 exception due to the 01 token.
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
json obj = json::object();
auto ptr = json::json_pointer("/a/b/01/d");
#ifdef INIT
obj[json::json_pointer("/a/b")] = json::object();
#endif
obj[ptr] = "value";
std::cout << obj << std::endl; // prints {"a":{"b":{"01":{"d":"value"}}}}
}
However, if the first part of the JSON object up to 01 is pre-initialized, then the '01' token is treated as a string and the code runs successfully.
- Is this expected behavior? It seems unintuitive to me. Regardless, I am currently taking advantage of this behavior by walking the json_pointer and pre-initializing to handle cases like this.
- Would it make sense to have a mode where all json_pointer tokens are always treated as strings?
Related: #4446
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the json_pointer overload of operator[] and the linked discussion, then reproduce the sample with and without INIT to compare token handling. Trace how the /01 token is interpreted during object creation. Done should include a documented resolution of the behavior and tests for both initialization paths, with any string-token mode explicitly scoped.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 45/100