How to convert dictionary to JSON?

0 votes
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
file.write(str(r['rating']))

I am not able to access my data in the JSON. What am I doing wrong?

TypeError: string indices must be integers, not str
Apr 15 in Python by kartik
• 26,540 points
517 views

1 answer to this question.

0 votes

Hello @kartik,

json.dumps() returns the JSON string representation of the python dict.

You can't do r['rating'] because r is a string, not a dict anymore

Perhaps you meant something like

r = {'is_claimed': 'True', 'rating': 3.5}
json = json.dumps(r) # note i gave it a different name
file.write(str(r['rating']))

Thank You!!

answered Apr 15 by Niroj
• 57,260 points

Related Questions In Python

+1 vote
1 answer

how to convert json to csv or to store in sql

You can convert JSON data to csv ...READ MORE

answered Jul 2, 2018 in Python by Nietzsche's daemon
• 4,260 points
358 views
0 votes
1 answer

How can I convert JSON to CSV?

I am not sure this question is ...READ MORE

answered Oct 1, 2018 in Python by aryya
• 7,380 points
8,455 views
0 votes
1 answer

How do you convert a dictionary to a defaultdict?

from collections import defaultdict a = {1: 2, ...READ MORE

answered Aug 2, 2019 in Python by Mohammad
• 3,210 points
3,006 views
0 votes
1 answer

How to convert CSV file to JSON file using Pandas?

Hi@akhtar, You can convert your CSV file to ...READ MORE

answered Jun 25 in Python by MD
• 55,020 points
311 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,349 views
0 votes
0 answers
0 votes
1 answer

How to convert a DictProxy object into JSON serializable dict?

Hello, Rather than using a private DictProxy method like _getvalue(), I'd ...READ MORE

answered Apr 15 in Python by Niroj
• 57,260 points
450 views
0 votes
1 answer