user29496
user29496

Reputation: 319

How to get value of nested dictionary in Python

I am trying to get value from a nested dictionary and i am getting string indices must be integers error.

Here is my dictionary

dict1 = {'ID': '123', 'Name': 'test', 'VersionId': '123423', 
          'String1': '{"key1":"value1","key2":"value2"}'}

I am trying to get the value2 but failing badly. Can anyone shed some light on this.

sub_dict = dict1['String1']
res = str(json.loads(sub_dict)) 
print(res['key2'])

if i print(res) this prints {'key1': 'value1', 'key2': 'value2'} but i am unable to get value2

Thanks,

Upvotes: 0

Views: 37

Answers (1)

Karthik Radhakrishnan
Karthik Radhakrishnan

Reputation: 944

You just overdid it.

sub_dict = dict1['String1']
res = json.loads(sub_dict)
print(res['key2'])

You do not need to convert res to a string.

Upvotes: 1

Related Questions