Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to indent json data
#1
I am trying to print the JSON with proper indentation and sorted keys, but it throws an error.

Need help to fix this

Complete code :

>>> import json
>>> import time
>>> for n in range(1, 10):
...   data = {'id': n, 't': float("%.3f" % time.time()), 'interest':['photo','tt']}
...   jsonData=json.dumps(data)
...   data = jsonData.encode('utf-8')
...   print(data (indent=4, sort_keys=True))
...
Error:
Traceback (most recent call last): File "<stdin>", line 5, in <module> TypeError: 'bytes' object is not callable >>>
Reply
#2
import json
import time
for n in range(1, 10):
    data = {'id': n, 't': float("%.3f" % time.time()), 'interest':['photo','tt']}
    json_data=json.dumps(data, indent=4, sort_keys=True)
    print(json_data)
Output:
{ "id": 1, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 2, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 3, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 4, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 5, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 6, "interest": [ "photo", "tt" ], "t": 1571751206.159 } { "id": 7, "interest": [ "photo", "tt" ], "t": 1571751206.16 } { "id": 8, "interest": [ "photo", "tt" ], "t": 1571751206.16 } { "id": 9, "interest": [ "photo", "tt" ], "t": 1571751206.16 }
Note that this prints 9 separate JSON strings, not single JSON string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks a lot for your reply.
Can you please let me know what was wrong in my approach ?
I was also doing the same thing but in single line. Isn't that allowed ?

Thanks in Advance.
Reply
#4
(Oct-22-2019, 01:38 PM)dataplumber Wrote: Can you please let me know what was wrong in my approach ?
I was also doing the same thing but in single line. Isn't that allowed ?
No, you were not doing the same thing in a single line
indent and sort_keys are arguments that you can pass to json.dumps()
In your snippet data is a bytes object and by using data() you were trying to call the object like a function (not possible) and getting the error and passing these arguments to it

Again, note that this is not single JSON string. in the loop you print 9 separate JSON strings.
I guess you may want it to be JSON array of JSON objects
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks for the explanation.

PS : I will keep in mind to keep the code and error in separate tags
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 180 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  getting unexpected indent errors trying to move cells up jensengt 4 862 Jun-28-2023, 12:05 PM
Last Post: deanhystad
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,442 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Read nested data from JSON - Getting an error marlonbown 5 1,351 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,070 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Unable to request image from FORM Data usman 0 985 Aug-18-2022, 06:23 PM
Last Post: usman
  Convert nested sample json api data into csv in python shantanu97 3 2,792 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,876 May-09-2022, 02:49 AM
Last Post: snippsat
  IndentationError: unexpected indent dee 3 2,294 May-02-2022, 02:15 AM
Last Post: dee
  json api data parsing elvis 0 920 Apr-21-2022, 11:59 PM
Last Post: elvis

Forum Jump:

User Panel Messages

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