问题标签 [ijson]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
28 浏览

python - 如何使用 ijson 从 json 文件中提取一组相应的数据?

我有一个这样的json文件:

我想提取 CVE-ID 和相应的 CPE,所以我可以通过 CPE lcoate CVE-ID,这是我的代码

结果:

但是上面这只是提取数据并没有对应关系。

有人可以帮忙吗?一点帮助将不胜感激。

0 投票
1 回答
206 浏览

python - Python ijson - 嵌套解析

我正在使用 JSON 的 Web 响应,看起来像这样(简化,我无法更改格式):

我想做两件事:

  1. 检查前两个对象而不将所有内容读入内存,我可以使用 Ijson 来做到这一点:
  1. 检查第三个对象而不将其全部存储在内存中。如果我再做next(parsed)一次,所有的“数据”数组都将被读入内存并变成一个字典,我想避免它。

  2. 检查数据数组而不将其全部加载到内存中。如果我不关心其他键,我可以这样做:

问题是,我需要在同一个流上完成所有这些操作。

理想情况下,将第三个对象作为类似文件的对象接收会很棒,我可以再次将其传递给 ijson,但这似乎超出了该 API 的范围。

我也可以将 ijson 替换为可以做得更好的库。

0 投票
1 回答
142 浏览

python - 如何使用 IJSON kvitems 访问 JSON 文件中的所有密钥?

我正在使用 ijson.kvitems 遍历我拥有的 JSON 文件中的所有键。

JSON 文件如下所示:

基于这个答案,我的代码的简化版本看起来像这样(v 也是字典):

我只能以这种方式从原始文件中流式传输/读取大约 94% 的密钥,试图弄清楚是否有办法获得剩余的 6%。

谢谢!!

0 投票
1 回答
193 浏览

python - 使用 ijson 解析大 JSON 时出现“解析错误:不允许的令牌...”

我正在尝试解析和筛选一个非常大的 JSON 文件,其中包含 9gb 大小的推文元数据。这就是我使用 ijson 的原因,因为这是社区最推荐的此类文件。它仍然很新,但我装配了这个函数,它应该根据某些条件将值存储到列表中。在遍历不同的 JSON 时,它向我显示以下错误:

我不确定我需要改变什么才能让它工作。在使用 Twarc 库对推文进行水合后,我得到了这个文件。我在下面附上了我的示例代码。有没有人遇到过这种情况?

示例代码:

0 投票
0 回答
55 浏览

python - 读取大型 JSON 文件时的垃圾值

我必须使用python读取一个大小为3 Gb的大型json文件。json文件中的数据之间有一个垃圾值'] ['。对于体积较小的文件,我使用下面的脚本来修剪垃圾值。

对于大型文件,我使用下面的脚本来读取文件,并在处理较小的文件时使用上面的脚本处理了以下错误。

脚本:

错误:

IncompleteJSONError: 解析错误: 尾随垃圾 82220.00,"NUMBER":1799106.00}][{"DATE":"2021092412504700000 (就在这里) ------^

我还使用了 pandas 的 read_json 来阅读它,但最终得到了同样的错误。关于如何修剪这个垃圾值的任何想法都会非常有帮助。我没有共享文件或一些样本,因为这些文件是在安全系统中使用的。

我已经尝试使用下面提到的文件包装类,但仍然再次出现内存错误

0 投票
1 回答
59 浏览

python - Python ijson - 解析错误:尾随垃圾 // bz2.decompress()

使用 ijson 解析 json 时遇到错误。

背景:我有一系列(大约 1000 个)推特数据的大文件,这些文件以“.bz2”格式压缩。我需要将文件中的元素放入 apd.DataFrame中以进行进一步分析。我已经确定了我需要获取的密钥。我很谨慎地发布推特数据。

尝试:我已经设法使用bz2.decompress以下代码解压缩文件:

这给出了以下错误:

两件事情:

  • 我的解压方法是否正确并为 ijson 解析提供了正确的文件类型(ijson 需要字节和 str)?
  • 是 JSON 错误吗?// 如果是 JSON 错误,是否可以开发某种错误处理程序来移动到下一个文件 - 如果是这样,任何建议都将不胜感激?

任何帮助将不胜感激。

谢谢你,詹姆斯

0 投票
1 回答
44 浏览

python - ijson kvitems unexpected behaviour

I'm using ijson to parse through large JSONs. I have this code, which should give me a dict of values corresponding to the relevant JSON fields:

I think the kvitems object is acting like a generator and only making it through one run-through (I get the expected values for 'id', but the other data_list keys in parsed_records are empty).

To get around this I tried to make a list of duplicate kv_gen's:

This gave me the same error. I think mutability may be a culprit here, but I can't use copy on the kvitems object to see if this fixes it.

I then tried to use itertools.cycle(), but this seems to be working in a way I don't understand:

Also, the below works (in the sense it gives me values that match what I see when I load a JSON with json.load()):

I'm just interested in why my function doesn't, especially when the records object is being run through multiple times above...


Edit for Rodrigo

You are not showing however how you find that your final dictionary has values for id but not for the other keys. I'm assuming it's only because you are iterating over the values under the parse_records['id'] values first. As you do so, the generator expression is then evaluated and the underlying kvitems generator is exhausted.

Yup, this is correct - I was converting each val to a list to check each key had a generator containing the same number of items, as I was worried a downstream zip operation might truncate some values if they had more objects than the smallest generator.

I didn't convert to a list in the function as I thought a generator would be a better object to return (less memory intensive etc), which I could then convert to a list of I needed to outside the function.

You say that your finally piece of code works as expected. This is the only bit that surprises me, specially if you really, really inspected (i.e., evaluated) all three of the generator expressions after you created them. If you could clarify if that's the case it would be interesting; otherwise if you created all three generator expressions, but then evaluated one or the other, then there are no surprises here (because of the "About result collection" explanation).

Basically, it gave me the values I was expecting when I ran through the items as a zipped collection of generators and appended the items to a list. But this might need some more investigation, the JSONs are quite convoluted so I might have missed something.

0 投票
2 回答
58 浏览

python - python ijson不能一次处理多个元素

我有数千个非常大的 JSON 文件,需要对特定元素进行处理。为了避免内存过载,我使用了一个名为ijson的 python 库,当我只处理 json 文件中的单个元素但当我尝试一次处理多个元素时它工作正常

IncompleteJSONError:解析错误:过早的 EOF

部分 JSON:

工作代码:(打开多个文件)

失败代码:(单个文件打开)

不确定这是 使用 python ijson 读取具有多个 json 对象的大型 json 文件的原因,而 ijson 无法同时处理多个 json 元素。

另外,让我知道任何其他 python 包或任何可以处理大尺寸 json 而没有内存问题的示例示例。

0 投票
0 回答
51 浏览

python - 使用 ijson 和 f.seek(0) 读取大文件花费太多时间

我有10 万个非常大的 JSON 文件,需要对特定元素进行处理。为了避免内存过载,我使用了一个名为ijson的 python 库,当我处理每个对象时,它工作得很好,f.seek(0)但它使我的处理非常慢。另外,如果我删除此f.seek(0)输出,则会出现错误

过早的EOF

部分 JSON:

}

当前代码:(多个f.seek(0))不想使用:)

输出:

不确定这是 使用 python ijson 读取具有多个 json 对象的大型 json 文件的原因,而 ijson 无法同时处理多个 json 元素。

另外,让我知道任何其他 python 包或任何可以处理大尺寸 JSON 而没有内存问题的示例示例。

编辑:如果我不使用parse_eventsf.seek(0),则仅row['AF-DomainCount']返回正确值,其他行数为 0

注意:这不是作业,而是我面临的现实生活问题。基本上,我需要某种解决方案来避免f.seek(0)多次使用 ijson 使我的脚本更快