Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to format conversion to JSON string in Python

4 Answers

0 votes
import json
 
lst = ["python", "java", "c++"]
 
jsn = json.dumps(lst, indent=5)
 
print(jsn)

 
'''
run:
 
[
     "python",
     "java",
     "c++"
]
 
'''

 



answered Nov 25, 2018 by avibootz
0 votes
import json
 
lst = ["python", "java", "c++", "php"]
 
jsn = json.dumps(lst, indent=5, separators=". ")
 
print(jsn)

 
'''
run:
 
[
     "python".
     "java".
     "c++".
     "php"
]

'''

 



answered Nov 25, 2018 by avibootz
0 votes
import json

o = {"name": "Fox",
     "age": 52,
     "work": True,
     "profession": "Programmer",
     "languages": ("Python", "C", "Java"),
     "boat": None,
     "company": [
         {"name": "Google", "salary": 14400},
         {"name": "Microsoft", "salary": 14400}
     ]
}

jsn = json.dumps(o, indent=5, separators=(". ", " = "))

print(jsn)

 
'''
run:
 
{
     "languages" = [
          "Python".
          "C".
          "Java"
     ].
     "name" = "Fox".
     "profession" = "Programmer".
     "company" = [
          {
               "name" = "Google".
               "salary" = 14400
          }.
          {
               "name" = "Microsoft".
               "salary" = 14400
          }
     ].
     "age" = 52.
     "work" = true.
     "boat" = null
}

'''

 



answered Nov 25, 2018 by avibootz
0 votes
import json

dic = {"name": "Fox", "profession": "Programmer", "salary": 17500}

jsn = json.dumps(dic, indent=5, separators=(". ", " = "), sort_keys=True)

print(jsn)

 
'''
run:
 
{
     "name" = "Fox".
     "profession" = "Programmer".
     "salary" = 17500
}

'''

 



answered Nov 25, 2018 by avibootz

Related questions

2 answers 187 views
1 answer 154 views
1 answer 110 views
...