I have the following JSON data the I need to read in Flask:
{
"NewList":[
{
"key" : "myvalue1",
"value" : "value1"
},
{
"key" : "myvalue2",
"value" : "value2"
},
{
"key" : "myvalu3",
"value" : "value4"
}
]
}
And I'm having trouble doing so. The code I currently have is as follows:
@app.route('/dataread', methods=['GET', 'POST'])
def dataread():
if(request.json):
myvalue1 = request.json['NewList']['myvalue1']
return str(myvalue1)
else:
return 'nothing'
But it isn't working. I'm getting the following error:
KeyError: 'NewList'
I know my syntax must be wrong but I can't figure how to fix it. I'm sorry for such a newb question. Please can you help.
Thanks.
myvalue1is not a valid key. Dictionaries can be indexed by keys, not by their values.request.json? Might reveal a structuring issue. Additionally, "myvalue1" is not a key in this dictionary, despite what you've named it in the original.{"NewList": { "key1": "value1", "key2": "value2", ..}}. Then access this usingrequest.json['NewList']['key1']wherekey1could be 'myvalue1' w.r.t your code