I'm very new to python.

I'm forming a json string taking input from various REST calls.

Something like:

{
   "-gammaid#10191-":{
      "domain":"Kids Interest",
      "product":"Project1"
   },
   "-gammaid#10382-":{
      "domain":"Weekend Classes",
      "product":"Project2"
   },
   "-gammaid#10442-":{
      "domain":"Knowledge Driven",
      "product":"Project3"
   },
   "-gammaid#10620-":{
      "domain":"Primary Education",
      "product":"Project4"
   },
   "-gammaid#10986-":{
      "domain":"Other Domain",
      "product":"Project5"
   },
   "-gammaid#10987-":{
      "domain":"Kids Interest",
      "product":"Project6"
   },
   "-gammaid#10996-":{
      "domain":"External Classes",
      "product":"Project7"
   },
   "-gammaid#11663-":{
      "domain":"Parent Interaction",
      "product":"Project8"
   }
}

As you see each key gammaid has in-turn a json structured value.

When I'm running my_json.get("-gammaid#11663-"), I'm getting

AttributeError: 'str' object has no attribute 'get'

2 Answers

2
AdamGold On

You need to parse it into a dictionary first:

import json

s = 'YOUR JSON STRING'

d = json.loads(s)
print(d["-gammaid#11663-"])
0
Klemen Koleša On

If you define it like this:

s={
   "-gammaid#10191-":{
      "domain":"Kids Interest",
      "product":"Project1"
   },
   "-gammaid#10382-":{
      "domain":"Weekend Classes",
      "product":"Project2"
   }

This is python dict. So you can just access values like this:

s["-gammaid#11663-"]
{'domain': 'Parent Interaction', 'product': 'Project8'}

If you need to actually get json object you can do it like this:

import json
json.loads(json.dumps(s))