Skip to content

Add validate function that reports JSON file line number in error? #156

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

Closed
cdeil opened this issue Apr 12, 2014 · 6 comments
Closed

Add validate function that reports JSON file line number in error? #156

cdeil opened this issue Apr 12, 2014 · 6 comments

Comments

@cdeil
Copy link
Contributor

cdeil commented Apr 12, 2014

Would it be possible to add a validate function that takes a JSON file name (or file handle or text ... something where line number info is available) and reports the line number if there is an error?

Maybe there's a trick to embed line number info in the dict generated by the JSON parser? Maybe build a second dict with identical structure, but line numbers as values instead of the actual content?

For my use case I don't care about performance (speed, memory), but it would be very helpful to get a line number in the error.

I'm new to JSON schema, so apologies if this is a stupid and impossible feature request.
The same question was asked before by @BrunoQC here: #90 (comment).

@Julian
Copy link
Member

Julian commented Apr 13, 2014

You're talking about line numbers for validation errors, not for invalid JSON presumably?

If so I think I feel the same as I did there -- there's a piece of that that seems like it's the responsibility of jsonschema and a piece that feels like it's the responsibility of a JSON parser. jsonschema will already tell you what the path in the instance is for the failing property. If you combine that with a JSON parser that preserves where that property was in the file, you seem to have what you want.

I don't want to get in the business of writing a JSON parser, but I'd bet there probably is one that could do that. I can give this some more thought probably, but that's the direction I'd think is worth pursuing.

@cdeil
Copy link
Contributor Author

cdeil commented Apr 13, 2014

If you combine that with a JSON parser that preserves where that property was in the file, you seem to have what you want.

Is it possible to do this with the json parser in the Python standard library?

@Julian Julian closed this as completed Jul 4, 2014
@analog-cbarber
Copy link

The JSON decoder from the standard python library does not provide a source location information. I am not aware of one that does.

@jdkandersson
Copy link

Full disclosure: I'm the maintainer of the package below.

There is now a new Python package that solves this use case: https://github.com/open-alchemy/json-source-map

Installation: pip install json_source_map

For example, the following JSON document:

{
    "foo": "bar"
}

Produces the following source map:

{
    "": Entry(
        value_start=Location(line=0, column=0, position=0),
        value_end=Location(line=2, column=1, position=20),
        key_start=None,
        key_end=None,
    ),
    "/foo": Entry(
        value_start=Location(line=1, column=11, position=13),
        value_end=Location(line=1, column=16, position=18),
        key_start=Location(line=1, column=4, position=6),
        key_end=Location(line=1, column=9, position=11),
    ),
}

@sspTest
Copy link

sspTest commented Aug 23, 2023

If you combine that with a JSON parser that preserves where that property was in the file, you seem to have what you want.

Is it possible to do this with the json parser in the Python standard library?

@cdeil did you find any package to get the line number for error cases?

@cdeil
Copy link
Contributor Author

cdeil commented Aug 23, 2023

@sspTest - I don't remember. I think we ended up using YAML instead of JSON and PyDantic. But I don't know if there we had good error messages mentioning the line number in the end or which YAML package we used.

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

No branches or pull requests

5 participants