Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json.dumps list output
#1
Is it possible in python 3.8 to print list in one line rather than printing each item on new line via json.dumps? Are there any other json libraries that can do it?
Reply
#2
json dumps converts to a dictionary.
this dictionary can be better viewed as a formatted text file.

python has a tool that you can use to convert and format JSON data
to a nicely formatted text file.

from command line, Linux format is:
cat myfile.json | python -m json.tool > myfile.txt
Reply
#3
(Mar-16-2020, 02:23 AM)Larz60+ Wrote: cat myfile.json | python -m json.tool > myfile.txt
But this is for command line. I need something that will print inner lists of my dict in one line. Like here https://stackoverflow.com/questions/2626...n/54356188 The answers are pretty old there, so maybe there is a new solution?
Reply
#4
Out of interest, why?
Reply
#5
(Mar-16-2020, 07:52 AM)ndc85430 Wrote: Out of interest, why?
Well, um, I just need this format to complete the task. This is what the "customer" wants.
Reply
#6
Ha, nice little printing exercise (one can print into file as well).

Using data from SO example:

>>> j = {'rows_parsed': [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'i']]}
>>> for k in j: 
...     print('{') 
...     print(f'{" " * 4}"{k}":  [') 
...     print(*[f'{" " * 8}{item}' for item in j[k]], sep=',\n') 
...     print(f'{" " * 4}]') 
...     print('}') 
...      
{
    "rows_parsed":  [
        ['a', 'b', 'c', 'd'],
        ['e', 'f', 'g', 'i']
    ]
}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
here's my dictionary display:
    def display_dict(dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1
Reply
#8
It seems that there's no an easy way to achieve one line printing in .json. Thanks for the answers!
Reply
#9
Have you looked at the documentation? https://docs.python.org/3/library/json.html
Reply
#10
Yes, but I haven't found a way to control inner lists output without writing my own encoder
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question convert unlabeled list of tuples to json (string) masterAndreas 4 646 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  How to append to list a function output? rama27 5 864 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  API JSON response missing list gives keyerror rolfmadsen 3 819 Mar-28-2020, 10:12 AM
Last Post: buran
  If item in list = true, Output = xx kroh 0 572 Feb-19-2020, 09:17 AM
Last Post: kroh
  Not Getting the Desired value using json.dumps saurabh210 0 604 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  json.dumps output error in python3 prayuktibid 2 852 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Output to a json file problem Netcode 3 1,198 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Difference in list output OokaydO 6 1,152 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  json.load causing IndexError: list index out of range WallyZ 5 2,775 May-20-2019, 01:11 PM
Last Post: WallyZ
  json.dumps to keep dictionary keys batchenr 1 762 May-14-2019, 11:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

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