gpt4 book ai didi

javascript - 如何 trim 除大型 JSON 文件引号内的所有空格

转载 作者:可可西里 更新时间:2023-11-01 11:49:09 26 4
gpt4 key购买 nike

我目前正在处理一个大型 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"}

除了引号内的空格外,基本上每个空格都应该删除。

最佳答案

您可以使用 jq-c--compact-output 选项:

jq -c '' < your-file.json

演示:

$ echo '
> {
> "a": "b"
> }' | jq -c ''
{"a":"b"}

关于javascript - 如何 trim 除大型 JSON 文件引号内的所有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com