Git Product home page Git Product logo

Comments (3)

rtobar avatar rtobar commented on July 21, 2024

@nisace thanks for the very thorough report.

I think there's a misalignment between the expectations you have for your particular JSON document and prefix, and how kvitems works for arbitrary JSON documents and prefixes. Consider the following two cases:

$> echo '[{"b": 1}, {"b": 1}]' | python -m ijson.dump -m kvitems -p item
#: key, value
-------------
0: b, 1
1: b, 1
$> echo '{"a": {"b": 1}, "a": {"b": 1}}' | python -m ijson.dump -m kvitems -p a
#: key, value
-------------
0: b, 1
1: b, 1

The first case is not uncommon, and the second is a bit weirder, in the sense that the input JSON document has duplicate keys -- but possible, as the JSON specification doesn't disallow duplicate keys.

These two examples should hopefully make it clear that kvitems's behavior should be to traverse the input document until exhaustion, as there's no way for it to know in advance when all matches for a given prefix have been found. Hence this is not really a bug, but a case of wrong expectations.

However....

I do see the opportunity to implement a new option for kvitems (e.g. stop_after_first or similar). Using this option, users could indicate that they know the prefix appears only once in the document. The option would default to False for the reasons explained. When set to True, kvitems would behave basically as you describe, yielding the key/value pairs off the prefix the first time around and then exiting.

Thoughts?

from ijson.

nisace avatar nisace commented on July 21, 2024

@rtobar thanks for your answer. Indeed, I understand your point and agree with the stop_after_first option as a solution. That would be great !

Not sure how to implement though :)

from ijson.

rtobar avatar rtobar commented on July 21, 2024

@rtobar thanks for your answer. Indeed, I understand your point and agree with the stop_after_first option as a solution. That would be great !

Not sure how to implement though :)

Yeah, I have some ideas but it could take a while. I'll close this ticket and create a new one to keep track of the potential new feature.

from ijson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.