Question

In a python script, create a list of dictionaries and write JSON from out of the...

In a python script, create a list of dictionaries and write JSON from out of the dictionaries.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import json

nameAgeDict = {'Jack': 13, 'Leena':25, 'Martin': 45}
nameCityDict = {'Jack': 'Alley', 'Leena': 'NYC', 'Martin': 'Sydney'}

dictList = [nameAgeDict, nameCityDict]

# using dumps, you can create JSON form any python object
print(json.dumps(dictList))

please upvote. Thanks!

Add a comment
Answer #2

CodeToCopy:

list_of_dicts_json.py

# importing json module

# will be used to convert list of dicts to json

import json

# creating list of dictionaries

list_of_dicts = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]

# printing list of dictionaries

print("List of dictionaries: ");

print(list_of_dicts)

# converting list of dictionaries to json

json_of_list_of_dicts = json.dumps(list_of_dicts)

#printing json string of list of dictionaries

print("Json of list of dictionaries: ");

print(json_of_list_of_dicts)

OutputScreenshot:

Add a comment
Know the answer?
Add Answer to:
In a python script, create a list of dictionaries and write JSON from out of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT