【问题标题】:Combine all JSON files in a directory into one file - Windows将目录中的所有 JSON 文件合并为一个文件 - Windows
【发布时间】: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 此数据不会是动态的,我需要提前将其组合起来,以便将其转换为我正在从事的机器学习项目的数据源。

标签: json windows jq


【解决方案1】:

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

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 文件的目录,但我收到此错误:“jq: error: Could not open file *.json: Invalid argument”我收到了这个之前尝试多个命令时出错。
  • 我在 Windows 上安装了 Ubuntu,因此我可以访问 linux shell,并且此响应运行良好。我使用了“jq -n '[inputs | .messages] | add' *.json > output.json”并将所有消息编译到一个新文件中。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多