I am using python
to load a json file and using jsonschema
to print errors according to the schema
i have prepared.
My question is how do i print a specific line of a json file from a loop:
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path) for error in errors: print(error.message, sep=", ")
The output i get is 'lending_details' is a required property
which is the error.message
.
What i want is to print: On line number 4 ,'lending_details' is a required property
Is there a way to count and display the specific line number of a json file?
Advertisement
Answer
In general, no, because by the time the JSON Schema evaluator sees the data instance, the data has been parsed from JSON text into a data structure and the line number information has been lost.
To make this work, you will need to have a JSON decoder that can associate line numbers with each section of the data in a way that the JSON Schema evaluator can later make use of it when generating its errors. For example, I could see a decoder turning this JSON:
{ "foo": { "hello": [ "a", "b", "c" ] "bar": true }
into this line number mapper:
{ "": 1, "/foo": 2, "/foo/hello": 3, "/foo/hello/0": 4, "/foo/hello/1": 5, "/foo/hello/2": 6, "/bar": 8 }
..and then when the JSON Schema evaluator is generating an error at data instance “/bar”, we can use this lookup table to insert “..at line 8” into the error.