Git Product home page Git Product logo

Comments (5)

raeldor avatar raeldor commented on July 22, 2024

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?

from ijson.

rtobar avatar rtobar commented on July 22, 2024

@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)

from ijson.

rtobar avatar rtobar commented on July 22, 2024

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

from ijson.

rtobar avatar rtobar commented on July 22, 2024

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

from ijson.

raeldor avatar raeldor commented on July 22, 2024

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.