Skip to content

Get text after JSON (parse with strict=false)#5049

Open
AbitTheGray wants to merge 1 commit intonlohmann:developfrom
AbitTheGray:parse_some
Open

Get text after JSON (parse with strict=false)#5049
AbitTheGray wants to merge 1 commit intonlohmann:developfrom
AbitTheGray:parse_some

Conversation

@AbitTheGray
Copy link

Hi, I had a need for parse where strict=false. To be explicit, I need to have a string which starts with JSON but is followed by a text.

{ "my_key": "my_value" }That was a cool Json!
["a", "b", "c"]What a nice Array.
[{ "my_key": "my_value" }, { "my_key": "another_value"}]Can we also do an array of objects?

Currently the only way I found is to use exceptions to control flow, which I don't like.

// https://godbolt.org/z/vveqod138
std::pair<json, std::size_t> parse_some(const std::string& input)
{
    try {
        // If the whole string is pure JSON, this succeeds.
        json j = json::parse(input);
        return {j, input.size()};
    } catch (const json::parse_error& e) {
        // e.byte is the 1-based index of the error location in the input.
        // In the "trailing text" case, that position is the first non-JSON char.
        std::size_t offset = (e.byte > 0) ? (e.byte - 1) : 0;

        // Try parsing up to the error position; if trailing text caused the error,
        // this substring is valid JSON.
        try {
            json j = json::parse(input.substr(0, offset));
            return {j, offset};
        } catch (...) {
            // Not a trailing-text situation; rethrow the original error.
            throw;
        }
    }
}

OK, there may be another one where I pre-parse it manually but I don't really see that as an option.
So I've tried implementing parse_some that returns not only the object but also the data after it (either as an offset, or span).

In the MR are my proposed changes, with no tests. Please tell me if you like the idea and way of doing it.

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

Read the Contribution Guidelines for detailed information.

@AbitTheGray AbitTheGray requested a review from nlohmann as a code owner January 9, 2026 22:38
@github-actions github-actions bot added the M label Jan 9, 2026
@gregmarr
Copy link
Contributor

gregmarr commented Jan 9, 2026

You can do this with the operator>>(std::istream& i, basic_json& j) function. That parses with strict set to false, so it accepts text after the closing }.
https://godbolt.org/z/1EjfbeP7c

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @gregmarr pointed out, this functionality should be possible with the existing API. No need to add another function for this.

@github-actions
Copy link

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

@github-actions github-actions bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

M state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants