【发布时间】:2019-03-03 23:31:38
【问题描述】:
我对 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"
}
]
参与者 2 代表我对消息的响应,其他参与者是我正在与之对话的人(Facebook 的原始 JSON 输出中就是这样)
【问题讨论】:
-
请尽可能遵循minimal reproducible example 准则。在不了解所需组合的更多信息的情况下,很难提供具体建议。
-
出于好奇,这些数据是动态的,还是您只需要提前合并文件?我也有类似的需求,只是最终使用 powershell 创建了 JSON 文件以供以后使用。
-
我们不是代码编写服务。我们很高兴能提供帮助,但您没有提供您尝试在 Windows 中输入命令时所做的事情,并且您没有提供收到的错误消息。如果您不向我们展示您在做什么,我们如何帮助您找出它们为什么不起作用?
-
在我的原始回复中添加了更多细节,我是新来在这个网站上发布我自己的问题,所以我很抱歉我在最初的帖子中缺少细节。
-
@CodeSpent 此数据不会是动态的,我需要提前将其组合起来,以便将其转换为我正在从事的机器学习项目的数据源。