如何检查JSON并添加/删除密钥
我有一个JSON文件作为NOSQL数据库的导出,以下示例是1xxx记录中的2个。
{
"task": {
"id1": {
"completed": true,
"title": "Testing",
"desc": "short desc",
"status": "completed",
"dueDate": {
"_seconds": 1622607310,
"_nanoseconds": 867000000
},
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": {
"_seconds": 1622607408,
"_nanoseconds": 365000000
},
"genre": "Errands"
},
"id2": {
"completed": true,
"title": "Testing 2",
"status": "completed",
"dueDate": {
"_seconds": 1622608576,
"_nanoseconds": 476999000
},
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": {
"_seconds": 1622608592,
"_nanoseconds": 628999000
},
"genre": "Errands",
"latLng": null
}
}
}
由于ID1和ID2中的键不一致,因此我试图找到一种方法/工具来确保在使用它们之前键保持一致。另外,当导出将日期推到秒时,我还试图将其转换为时间戳/字符串,
最终输出理想情况应该是:
{
"task": {
"id1": {
"completed": true,
"title": "Testing",
"desc": "short desc",
"status": "completed",
"dueDate": "Wednesday, 2 June 2021 12:15:10.867 GMT+08:00",
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": "Wednesday, 2 June 2021 12:16:48.365 GMT+08:00",
"genre": "Errands"
},
"id2": {
"completed": true,
"title": "Testing 2",
"desc": "",
"status": "completed",
"dueDate": "Wednesday, 2 June 2021 12:36:16.476 GMT+08:00",
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": "Wednesday, 2 June 2021 12:36:32.628 GMT+08:00",
"genre": "Errands"
}
}
任何帮助都会受到赞赏
1:我找到了很棒的JQ,但我无法写入一个足够好的查询:
. as $data |
paths(scalars) | . as $path |
"\($path[1]):{\($path[2]):\($data | getpath($path))}"
这给了我:
"id1:{completed:true}"
"id1:{title:Testing}"
"id1:{desc:short desc}"
"id1:{status:completed}"
"id1:{dueDate:1622607310}"
"id1:{dueDate:867000000}"
"id1:{citizenID:Uvr0vZqIPON5UZsgJsMYPe2Qfsi2}"
"id1:{datePosted:1622607408}"
"id1:{datePosted:365000000}"
"id1:{genre:Errands}"
"id2:{completed:true}"
"id2:{title:Testing 2}"
"id2:{status:completed}"
"id2:{dueDate:1622608576}"
"id2:{dueDate:476999000}"
"id2:{citizenID:Uvr0vZqIPON5UZsgJsMYPe2Qfsi2}"
"id2:{datePosted:1622608592}"
"id2:{datePosted:628999000}"
"id2:{genre:Errands}"
它离理想的输出还很遥远
I have a JSON file as an export of a nosql database, the sample below is 2 of 1xxx records.
{
"task": {
"id1": {
"completed": true,
"title": "Testing",
"desc": "short desc",
"status": "completed",
"dueDate": {
"_seconds": 1622607310,
"_nanoseconds": 867000000
},
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": {
"_seconds": 1622607408,
"_nanoseconds": 365000000
},
"genre": "Errands"
},
"id2": {
"completed": true,
"title": "Testing 2",
"status": "completed",
"dueDate": {
"_seconds": 1622608576,
"_nanoseconds": 476999000
},
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": {
"_seconds": 1622608592,
"_nanoseconds": 628999000
},
"genre": "Errands",
"latLng": null
}
}
}
As the keys inside id1 and id2 are not consistent, im trying to find a way/tool to make sure that the keys are consistant before i use them. also, as the export push the date to as in seconds, im also trying to convert it to a timestamp/string
The final output ideally should be:
{
"task": {
"id1": {
"completed": true,
"title": "Testing",
"desc": "short desc",
"status": "completed",
"dueDate": "Wednesday, 2 June 2021 12:15:10.867 GMT+08:00",
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": "Wednesday, 2 June 2021 12:16:48.365 GMT+08:00",
"genre": "Errands"
},
"id2": {
"completed": true,
"title": "Testing 2",
"desc": "",
"status": "completed",
"dueDate": "Wednesday, 2 June 2021 12:36:16.476 GMT+08:00",
"citizenID": "Uvr0vZqIPON5UZsgJsMYPe2Qfsi2",
"datePosted": "Wednesday, 2 June 2021 12:36:32.628 GMT+08:00",
"genre": "Errands"
}
}
Any help is appreciated
Edit 1: I found the wonderful jq, but im not able to write a good enough query:
. as $data |
paths(scalars) | . as $path |
"\($path[1]):{\($path[2]):\($data | getpath($path))}"
So this gives me:
"id1:{completed:true}"
"id1:{title:Testing}"
"id1:{desc:short desc}"
"id1:{status:completed}"
"id1:{dueDate:1622607310}"
"id1:{dueDate:867000000}"
"id1:{citizenID:Uvr0vZqIPON5UZsgJsMYPe2Qfsi2}"
"id1:{datePosted:1622607408}"
"id1:{datePosted:365000000}"
"id1:{genre:Errands}"
"id2:{completed:true}"
"id2:{title:Testing 2}"
"id2:{status:completed}"
"id2:{dueDate:1622608576}"
"id2:{dueDate:476999000}"
"id2:{citizenID:Uvr0vZqIPON5UZsgJsMYPe2Qfsi2}"
"id2:{datePosted:1622608592}"
"id2:{datePosted:628999000}"
"id2:{genre:Errands}"
which is still far from the ideal output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定输入文件作为
input.json
和以下JQ脚本为transform.jq
:此命令
返回此输出:
当然您可以将完整的脚本挤入一个命令行中争论。
缺少事物:
_nanoseconds
被忽略。您可以读取 jq 的日期/时间处理链接到使用的C函数。缺少不必要的
任务
级别。如果您真的需要它,请用.task |
.task | =
将其恢复。什么“一致的键”意味着您可以在
map_values
中完成。Given your input file as
input.json
and the following JQ script astransform.jq
:This command
returns this output:
Of course you can squeeze the complete script into one command line argument.
Missing things:
The
_nanoseconds
are ignored. You can readup the date/time handling of JQ and follow the links to the used C functions.The unnecessary
task
level is missing. If you really need it, replace.task |
with.task |=
to get it back.Whatever "consistent keys" mean to you can be done inside
map_values
.