简体   繁体   中英

How to call flask jsonify on an array of key-value pairs

I am trying to use jsonify to construct an object from two 1d arrays where the first array (keys) is a list of all the keys, and the second is a list of their respective values (vals). I want to call jsonify({ data: arr }) and get something that looks like this

{ data: [
{ key1 : val1 },
{ key2 : val2 }
]}

etc. Is there a way to do this? At first I was hoping I could construct arr using something like this:

arr = [str(keys[p]) + "=" + str(vals[p]) for p in range(keys.length)]

but this obviously doesn't work. Is there a workaround I can use here?

Edit: Should I try doing something like jsonify({ data: dict }) where dict is a dictionary mapping keys to vals?

data = [ {k:v} for k,v in zip(["key1","key2"],["a","b"]) ]
return jsonify({"data" : data})

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