How do I parse ndjson files?

Viewed 1432

I performed a GET request from lichess.org, API reference: https://lichess.org/api#operation/apiGamesUser

import requests
import json
import ndjson
response = requests.get('https://lichess.org/api/games/user/ardito_bryan', params={'max':10})

I am unable to decode ndjson, python library here: https://pypi.org/project/ndjson/ According to the library reference I should be using:

items = response.json(cls=ndjson.Decoder)

It does not work, runs error:

JSONDecodeError: Expecting value: line 1 column 3 (char 2)

Ty.

1 Answers

Fixed it by adding in the following header to the request.

headers={
'Authorization': f'Bearer {token}', # Need this or you will get a 401: Not Authorized response
"Accept": "application/x-ndjson"
}

After this I convert the ndjson by:

resp_json = []
ndjson = response.content.decode().split('\n')

for json_obj in ndjson:
    if json_obj:
        resp_json.append(json.loads(json_obj))

Now you will have your parsable object.

Related