Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ijson.parse iter_lines() returns error too many values to unpack (expected 2) #58

Closed
raeldor opened this issue Sep 12, 2021 · 5 comments
Labels
question Further information is requested

Comments

@raeldor
Copy link

raeldor commented Sep 12, 2021

Me again, sorry. I'm trying to stream data from a web call directly into ijson. However, if I use...

parser=ijson.parse(cellset_response.iter_lines())
for prefix, event, value in parser:
pass

I get 'too many values to unpack (expected 2)' error. If I use iter_content() instead of iter_lines() I get 'not enough values to unpack (expected 2, got 1).

It seems I can't win. :D

@raeldor raeldor added the question Further information is requested label Sep 12, 2021
@raeldor
Copy link
Author

raeldor commented Sep 12, 2021

Just to add a bit more detail, iter_lines() seems to give the entire stream as a binary string since the JSON is not formatted with newline characters. iter_content() gives the data in a stream format returning what looks like 1 or 2 binary characters at a time.

I thought ijson was able to handle a stream. Surely that stream doesn't need to be formatted?

@raeldor raeldor changed the title ijson.parse iter_lines() returns error too manu values to unpack (expected 2) ijson.parse iter_lines() returns error too many values to unpack (expected 2) Sep 12, 2021
@rtobar
Copy link

rtobar commented Sep 12, 2021

@raeldor my previous advice was to build something around iter_content, not to pass it (or iter_lines) directly with ijson. As mentioned before, ijson works with file-like objects; that is, objects with a read method, so you'd need to wrap the response to make it look like a file that can be read.

Something like this:

import ijson
import requests

class ResponseAsFileObject:
    def __init__(self, url):
        response = requests.get(url, stream=True)
        self.data = response.iter_content(chunk_size=65536)
    def read(self, n):
        if n == 0:
            return b''
        return next(self.data, b'')

URL = 'your-url'
for prefix, event, name in ijson.parse(ResponseAsFileObject(URL)):
    print(prefix, event, name)

@rtobar
Copy link

rtobar commented Sep 14, 2021

@raeldor please let me know if this worked so we can close this.

@rtobar
Copy link

rtobar commented Sep 19, 2021

There hasn't been a response in a week, and the solution worked for me, so I'm closing this.

@rtobar rtobar closed this as completed Sep 19, 2021
@raeldor
Copy link
Author

raeldor commented Sep 19, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants