nlohmann / nlohmann/json

Behavior differences in operator[] object creation with a json_pointer

Open
#5,357 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

state: please discuss
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.

  1. 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.
  2. Would it make sense to have a mode where all json_pointer tokens are always treated as strings?

Related: #4446

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.