我目前正在处理一个大型JSON文件,并希望通过删除不在引号内的所有额外空格,制表符,返回等来缩短它。该文件大约有100,000行代码,我的其他脚本很难快速使用它。该文件最初看起来像:
{
"path": "/math/",
"id": "math",
"title": "Math Title",
"icon_url": "/images/power-mode/badges/circles-40x40.png",
"contains": [
"Topic",
"Video",
"Exercise"
],
"children": [],
"parent_id": "root",
"ancestor_ids": [
"root"
],
"description": "null",
"kind": "Topic",
"h_position": -10,
"v_position": 6,
"slug": "math"
}
我希望在删除不必要的空格,制表符,返回等之后看起来像这样:
{"path":"/math/","id":"math","title":"Math Title","icon_url":"/images/power-mode/badges/circles-40x40.png",
"contains":["Topic","Video","Exercise"],"children":[],"parent_id":"root","ancestor_ids":["root"],
"description": "null","kind":"Topic","h_position":-10,"v_position":6,"slug":"math"}
除了引号内的内容之外,基本上应删除每个空格。
答案 0 :(得分:2)
您可以将json读入代码,然后将其输出到指定紧凑格式的文件,引号内的空格将保留在字符串中。
在python中,您可以使用本机json库
import json
json.loads(your filestream)
json.dumps(your output stream) // the native output of json.dumps is compact
python docs https://docs.python.org/2/library/json.html
中的详细信息但是你应该能够在处理json的任何语言中使用相同的技术。
答案 1 :(得分:2)
您可以使用jq的-c
或--compact-output
选项:
jq -c '' < your-file.json
演示:
$ echo '
> {
> "a": "b"
> }' | jq -c ''
{"a":"b"}
答案 2 :(得分:1)
为什么不直接通过perl ???
perl -0pe 's#((^[^"]+")|("[^"]+$)|("[^"]+")|(^[^"]+$))#($x=$1)=~s/\s+/ /g;$x#ge'
代码基本匹配:
然后用一个空格替换所有一个或多个空白字符集。
基本上replace spaces only in between quotation marks略有修改......
答案 3 :(得分:0)
您可以使用JSON minifier online。 在Google上搜索JSON minifier。 Google
以下是JSON minifier返回给我的内容:
{"path":"/math/","id":"math","title":"Math Title","icon_url":"/images/power-mode/badges/circles-40x40.png","contains":["Topic","Video","Exercise"],"children":[],"parent_id":"root","ancestor_ids":["root"],"description":"null","kind":"Topic","h_position":-10,"v_position":6,"slug":"math"}
您可以看到它不会删除引号之间的空格。例如&#34;数学标题&#34;