Skip to content
Advertisement

How can i print the specific line number of a json file

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:

JavaScript

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:

JavaScript

into this line number mapper:

JavaScript

..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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement