Blanca
Blanca

Reputation: 21

Access JSON to DICT in Python

I have the next JSON in JS

var intervalos= {
          "operandos":[{
             "extremoInferior":$(edit_numvar).context.value,
             "extremoSuperior":$(edit_numvar2).context.value
          },
          {
             "extremoInferior":$(edit_numvar3).context.value,
             "extremoSuperior":$(edit_numvar4).context.value
          }]
};

and I did parsed_input = json.loads(self.intervalos)

but now I don't know how to access to my dict. I tried with

intervalos[operandos][extremoInferior])     

but it returns an error.

Could you help me for accessing to any element of my dict?

Upvotes: 2

Views: 118

Answers (3)

Pralhad Narsinh Sonar
Pralhad Narsinh Sonar

Reputation: 1454

Please try this code:

import simplejson as json

json_string = """{"operandos":[
                    {
                        "extremoInferior":"somevalue1",
                        "extremoSuperior":"somevalue2"
                    },
                    {
                        "extremoInferior":"somevalue3",
                        "extremoSuperior":"somevalue4"
                    }]
                }"""

json_data = json.loads(json_string)

print "Complete Dict: "
print json_data

print "operandos -> extremoInferior: "
print json_data['operandos'][0]['extremoInferior']

Upvotes: 2

Georg Mavridis
Georg Mavridis

Reputation: 2331

intervalos['operandos'][0]['extremoInferior']

or

intervalos['operandos'][1]['extremoInferior']

intervalos['operandos'] is defined as an array of two of these objects.

Upvotes: -2

Delimitry
Delimitry

Reputation: 3027

After deserializing JSON to Python object you could simply access to elements. For example:

parsed_input = json.loads(self.intervalos)
extremo_inferior_0 = parsed_input['operandos'][0]['extremoInferior']

Upvotes: 2

Related Questions