Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json to ndjson
#5
Thank you both of you

This is how my code is
response = requests.get(url, params=params, auth=('user', 'pass'))
#
print (response.headers['content-type'])
--> this says 'application/json; charset=utf-8'

json_response = response.json()
print (response.json())
### out put of this is like this
{'page': 1, 'per_page': 20, 'total': 9, 'saved_objects': [{'type': 'dashboard',................ 'version': 'WzM5OTEsM10='}]}

data = json.loads(json_response)
print (ndjson.dumps(data))
this last two line give me this error
File "export_output.py", line 35, in <module>
data = json.loads(json_response)
File "/usr/lib64/python3.6/json/__init__.py", line 348, in loads
'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'


This tells me this is not json string

ignore above it works now. Thank you guys.
it was typo on my part.

here is what my final code is
Actually found out that output of my request.get was ndjson already.
then use your logic for seperate out lines.

items = response.json(cls=ndjson.Decoder)
print (items)
print()
output = ndjson.dumps(items)
print (output)
Reply


Messages In This Thread
json to ndjson - by pythonlearner1 - May-21-2020, 10:32 PM
RE: json to ndjson - by bowlofred - May-22-2020, 01:22 AM
RE: json to ndjson - by macfanpl - May-22-2020, 07:48 AM
RE: json to ndjson - by buran - May-22-2020, 08:01 AM
RE: json to ndjson - by macfanpl - May-27-2020, 10:46 AM
RE: json to ndjson - by pythonlearner1 - May-22-2020, 02:29 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020