gpt4 book ai didi

json - 将一个目录中的所有 JSON 文件合并到一个文件中 - Windows

转载 作者:可可西里 更新时间:2023-11-01 10:31:56 25 4
gpt4 key购买 nike

我是 JSON 的新手,目前我有一个包含 ~130 个 JSON 文件的列表,这些文件是我通过下载我的 Facebook 消息数据收到的。我目前正在尝试使用 JQ 将它们全部连接到一个 json 文件中,同时保持现有消息顺序不变,但是当我尝试在 Windows 上输入命令时遇到错误。我已尝试在常见问题解答中遵循他们对 Windows 的建议,但仍然遇到问题。

所有文件都具有相同的布局

{
"participants": [
{
"name": "Participant One"
},
{
"name": "Participant Two"
}
],
"messages": [
{
"sender_name": "Participant One",
"timestamp_ms": 99999999999,
"content": "message content",
"type": "Generic"
},
{
"sender_name": "Participant Two",
"timestamp_ms": 9999999999,
"content": "message content",
"type": "Generic"
}
],
"title": "chat title",
"is_still_participant": true,
"thread_type": "Regular",
"thread_path": "thread path"
}

如果可以使用 JQ,我只想将“消息”合并到一个 JSON 文件中,同时保持它们的现有顺序。这些消息是这些文件中我唯一关心的数据。

--编辑--非常抱歉原始帖子中缺少详细信息。我已经尝试了几个在这里和其他地方找到的命令:

jq -s "[.[][]]" a-*.json > output.json

jq -s "{ attributes: map(.attributes[0]) }" file*.json > output.json

jq -s "*" *.json > output.json

我还尝试将此代码放入文本文件中并使用以下命令对其进行运行: jq -f 文件名但我也收到错误

我遇到的错误是:

jq: error: Could not open file *.json: Invalid argument

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>

至于我试图实现的输出的更多细节:我使用的是 JQ 1.5。我不关心 JSON 文件相互添加的顺序,只要消息保持正确的顺序,这样我就可以获得明确定义的消息/响应对。我也不在乎它们是否全部合并,我必须以不同的方式分离消息。类似这样的东西将是我理想的输出:

"messages": [
{
"sender_name": "Participant One",
"timestamp_ms": 99999999999,
"content": "message content",
"type": "Generic"
},
{
"sender_name": "Participant Two",
"timestamp_ms": 9999999999,
"content": "message content",
"type": "Generic"
},
{
"sender_name": "Participant Three",
"timestamp_ms": 9999999999,
"content": "message content",
"type": "Generic"
},
{
"sender_name": "Participant Two",
"timestamp_ms": 9999999999,
"content": "message content",
"type": "Generic"
}
]

参与者二代表我对消息的回复,其他参与者是我正在与之交谈的人(这在 Facebook 的原始 JSON 输出中是这样的)

最佳答案

您可以从以下命令行调用开始:

jq -n "[inputs | .messages] | add" *.json 

这有许多假设。如果您的 Windows shell 不支持通配符扩展,请参阅 Passing multiple wildcard filenames to a command in Windows和/或 https://superuser.com/questions/460598/is-there-any-way-to-get-the-windows-cmd-shell-to-expand-wildcard-paths/460648#460648

其他假设是您使用的是 jq 1.5 或更高版本,当然当前目录中的所有 *.json 文件都是相关的,并且 *.json 以所需的顺序列出它们。

如果 JSON 文件的顺序最好通过明确列出它们来确定,那么您可能需要创建一个批处理文件。如果它们的排序是由它们的内容决定的,那么你可以使用 jq 来为你排序,但是如何做的细节将取决于排序标准的细节。

关于json - 将一个目录中的所有 JSON 文件合并到一个文件中 - Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52547351/

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