我有1000多个代表游戏元素的JSON文件.这是一个典型的例子:
{
"name": "Reeve Hunter",
"edition": 3,
"revision": "2021 v1",
"keywords": [
"Circle",
"Wolf Sworn",
"Reeve of Orboros",
"Solo"
],
"point cost": 4,
"field allowance": 2,
"models": [
{
"Hunter": {
"spd": 6,
"str": 6,
"mat": 6,
"rat": 7,
"def": 13,
"arm": 13,
"cmd": 7,
"boxes": 5,
"advantages": [
"Advance Deploy",
"Assault",
"Pathfinder"
],
"base size": 30,
"attacks": [
{
"Double Crossbow": {
"type": "ranged",
"rng": 12,
"rof": 2,
"pow": 10
}
},
{
"Battle Blade": {
"type": "melee",
"rng": 0.5,
"pow": 3,
"p+s": 9
}
},
{
"Cleft Sword": {
"type": "melee",
"rng": 1,
"pow": 5,
"p+s": 11,
"weapon qualities": ["Weapon Master"],
"abilities": ["Powerful Charge"]
}
}
],
"abilities": [
"Hunter",
"Leadership [Reeves of Orboros]",
"Quickwork",
"Sprint"
]
}
}
]
}
虽然上面的JSON是有效的,但一旦我将所有JSON文件放入MongoDB数据库中,我就意识到我无法正确分析我的数据,因为models
和attacks
数组中的每个子文档都有一个唯一的名称作为键.
我想像这样转换每个JSON文档:
{
"name": "Reeve Hunter",
"edition": 3,
"revision": "2021 v1",
"keywords": [
"Circle",
"Wolf Sworn",
"Reeve of Orboros",
"Solo"
],
"point cost": 4,
"field allowance": 2,
"models": [
{
"model name": "Hunter",
"spd": 6,
"str": 6,
"mat": 6,
"rat": 7,
"def": 13,
"arm": 13,
"cmd": 7,
"boxes": 5,
"advantages": [
"Advance Deploy",
"Assault",
"Pathfinder"
],
"base size": 30,
"attacks": [
{
"attack name": "Double Crossbow",
"type": "ranged",
"rng": 12,
"rof": 2,
"pow": 10
},
{
"attack name": "Battle Blade",
"type": "melee",
"rng": 0.5,
"pow": 3,
"p+s": 9
},
{
"attack name": "Cleft Sword",
"type": "melee",
"rng": 1,
"pow": 5,
"p+s": 11,
"weapon qualities": [
"Weapon Master"
],
"abilities": [
"Powerful Charge"
]
}
],
"abilities": [
"Hunter",
"Leadership [Reeves of Orboros]",
"Quickwork",
"Sprint"
]
}
]
}
我很擅长使用awk
和sed
来清理文本,但是这个转换很复杂,而且有足够多的"问题",我觉得我需要一个不同的策略.
我一直在使用出色的工具jq
操作和查询JSON文件,我觉得必须有一种方法来利用它的能力来做我需要的事情.
有没有人对编写脚本的最佳策略有什么建议,该脚本可以将我所有的数据转换为我需要的更干净的格式?