0

So after receiving JSON with the following code:

ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'application/JSON':
    length = int(self.headers.get('content-length'))
    input = JSON.loads(self.rfile.read(length))

I received this kind of JSON from print(input) which is a dictionary:

{'packageInput': {'dog': [4.18, 0.08, 41.51, 0, 0, 0, 0, 0],
                  'cat': [2.752, 3.69, 2.29, 0, 0, 0, 0, 0],
                  'bird': [4.45, 4.97, 2.37, 0, 0, 0, 0, 0],
                  'chicken': [0.81, 0.61, '0.336', 0, 0, 0, 0, 0],
                  'min': ['1', 0, 0, 0, 0, 0, '5', 0, 0, 0, 0, 0],
                  'max': [4, 4, 4, 4, 100, 100, 100, 0, 0, 0, 0, 0]}}

As we can see, each "list" in the JSON has "array". How do I extract them into separate python list variable?

such as:

dog = []
cat = []
bird = []
chicken = []

I have been researching solutions, there are ranging from using for loops to modules import. I believe there is a much more simple solution to extract this.

Would you please share an idea?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    Having dynamically named variables is usually a bad idea. You already have something very close which is much better. By doing `variables = data['packageInput']` you can access each list as `data['dog']`, `data['cat']` etc... – Tomerikoo Aug 01 '21 at 14:00

2 Answers2

1

Like the comments on your question mention, you already have a dictionary which is probably the correct data structure for this. However, if you have a need to pull out a bunch of things from a dict as separate items, operator.itemgetter is a convenient, readable way to do this:

from operator import itemgetter

jsn = {'packageInput': {'dog': [4.18, 0.08, 41.51, 0, 0, 0, 0, 0], 'cat': [2.752, 3.69, 2.29, 0, 0, 0, 0, 0], 'bird': [4.45, 4.97, 2.37, 0, 0, 0, 0, 0], 'chicken': [0.81, 0.61, '0.336', 0, 0, 0, 0, 0], 'min': ['1', 0, 0, 0, 0, 0, '5', 0, 0, 0, 0, 0], 'max': [4, 4, 4, 4, 100, 100, 100, 0, 0, 0, 0, 0]}}

dog,cat,bird,chicken = itemgetter('dog','cat','bird','chicken')(jsn['packageInput'])

On the other hand, if you are looking for a dynamic way to do this — you don't know what variables will be created until you have the json — that's a mistake. It just doesn't work — after all you need to know the variable names later to use them later in the program. Using print(cat) later in the program would crash if "cat" was not in the json. A dictionary lets you deal with this.

Mark
  • 90,562
  • 7
  • 108
  • 148
0

exec is what you looking for, but you should avoid using it at any cost. Refer this.

d = {'packageInput': {'dog': [4.18, 0.08, 41.51, 0, 0, 0, 0, 0], 'cat': [2.752, 3.69, 2.29, 0, 0, 0, 0, 0], 'bird': [4.45, 4.97, 2.37, 0, 0, 0, 0, 0], 'chicken': [0.81, 0.61, '0.336', 0, 0, 0, 0, 0], 'min': ['1', 0, 0, 0, 0, 0, '5', 0, 0, 0, 0, 0], 'max': [4, 4, 4, 4, 100, 100, 100, 0, 0, 0, 0, 0]}}
obj = d.get('packageInput')
for k in obj.keys():
    exec(f"{k} = {obj.get(k)}")
print(dog)
#[4.18, 0.08, 41.51, 0, 0, 0, 0, 0]

Recommended Solution, use it as dict which already it is.

dog = obj.get('dog')
RG_RG
  • 349
  • 5
  • 8