wardi / jsonlines

Documentation for the JSON Lines text file format
http://jsonlines.org
139 stars 34 forks source link

A web validator for jsonlines #21

Closed hkasera closed 1 year ago

hkasera commented 8 years ago

A validator to validate .jsonl files or the content just like jsonlint

brontide commented 7 years ago

How about a 1 liner?

python -c 'import sys;import json;[None for line in sys.stdin if json.loads(line)]'

Would be better if it could display the line number and offending line, but exit 0 means it contains no errors.

sp4ce commented 1 year ago

I think we could have one in javascript, so we could easily put it into a webpage. Writing it down here, but this would be something like:

let lines = jsonl.split('\n');
let errors = '';
for (let i = 0; i < lines.length; i++) {
    let line = lines[i]
    if (i !== lines.length -1 && lines !== '') {
        try {
            JSON.parse(line);
        } catch(e) {
            if (e instanceof SyntaxError) {
                errors += `line ${i}: error ${e}\n`;
            } else {
                throw e;
            }
        }
    }
}

if (errors !== '') {
    console.log(errors)
} else {
    console.log('valid jsonl');
}
sp4ce commented 1 year ago

Hi, I opened a pull request for the web validator on the website, please have a look

CC @wardi