简体   繁体   中英

Extract fields from a JSON in Python(Django)

Hello I am new to the python world, and I am learning, I am currently developing a WebApp in Django and I am using ajax for sending requests, what happens is that in the view.py I get a JSON, from which I have not been able to extract the attributes individually to send to a SQL query, I have tried every possible way, I appreciate any help in advance.

def profesionales(request):
    body_unicode = request.body.decode('utf-8')
    received_json = json.loads(body_unicode)
    data = JsonResponse(received_json, safe=False)

    return data 

Data returns the following to me

{opcion: 2, fecha_ini: "2021-02-01", fecha_fin: "2021-02-08", profesional: "168", sede: "Modulo 7", grafico: "2"} 

This is the answer I get and I need to extract each of the values of each key into a variable

You can interpret this as dict.

for key in received_json:
   print(key,received_json[key])
   # do your stuff here


but if it's always a object with same keys (fixed keys), you can access directly:

key_data = received_json[key]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM