在 Unix 中验证 JSON 文件的语法?
Validate the syntax of JSON file in Unix?
有没有办法在 Unix 中验证 JSON 文件的语法并将无效文件移动到错误文件夹中。
谢谢,
凯文
您可以尝试使用 python oneliner:
python -c "import json;file = open('foo.json');json.loads(file.read());file.close()"
通过一些调整,您可以将其变成别名:
testjson(){
python -c "import json;file = open('');json.loads(file.read());file.close()"
}
并使用这样的东西:
ls | while read file;
testjson $file || mv $file ~/bad_json/
done;
一些示例:
echo "{}" > foo.json
echo "{" > bar.json
testjson bar.json && echo ok
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)
testjson bar.json || echo ko
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)
ko
testjson foo.json && echo ok
ok
testjson foo.json || echo ko
注意:请注意包含 '
或 \n
的特殊文件名
正在(在某种程度上)验证我的 test_0009 目录中的 1660 个 jsonld 文件。全部都有“0”退出代码(=“确定”):
[pjalajas@sup-pjalajas-2 504Project]$ find test_0009 -iname "*.jsonld" | parallel "jq '.' 2> /dev/null ; echo $?" | cut -d\ -f1 | sort | uniq -c | sort -k1nr
1660 0
有没有办法在 Unix 中验证 JSON 文件的语法并将无效文件移动到错误文件夹中。
谢谢, 凯文
您可以尝试使用 python oneliner:
python -c "import json;file = open('foo.json');json.loads(file.read());file.close()"
通过一些调整,您可以将其变成别名:
testjson(){
python -c "import json;file = open('');json.loads(file.read());file.close()"
}
并使用这样的东西:
ls | while read file;
testjson $file || mv $file ~/bad_json/
done;
一些示例:
echo "{}" > foo.json
echo "{" > bar.json
testjson bar.json && echo ok
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)
testjson bar.json || echo ko
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)
ko
testjson foo.json && echo ok
ok
testjson foo.json || echo ko
注意:请注意包含 '
或 \n
正在(在某种程度上)验证我的 test_0009 目录中的 1660 个 jsonld 文件。全部都有“0”退出代码(=“确定”):
[pjalajas@sup-pjalajas-2 504Project]$ find test_0009 -iname "*.jsonld" | parallel "jq '.' 2> /dev/null ; echo $?" | cut -d\ -f1 | sort | uniq -c | sort -k1nr
1660 0