-1

Is there a simple way in python/flask to jsonify an array of strings as a key/value pair?

So far I understood, that I can jsonify with following code:

fruits = ["apple", "pear", "melon"]
return jsonify(fruits)

which returns

["apple", "pear", "melon"]

Now, what is the simplest way to get this as a key/value like

{
    "fruits": ["apple", "pear", "melon"]
}
kxell2001
  • 131
  • 1
  • 1
  • 9

3 Answers3

1

Is this what you are looking for?

let fruits = ["apple", "pear", "melon"]
let fruitsObj = { fruits }
let json = JSON.stringify(fruitsObj)
twharmon
  • 4,153
  • 5
  • 22
  • 48
0

try this simple convertion

fruits = ["apple", "pear", "melon"]

function newFruits (array){
  return {fruits: array}
}

console.log(newFruits(fruits))
ironCat
  • 161
  • 6
0

Look at this answer for an explanation as to why this isn't simple to implement.

Instead, try:

import json
obj_dict = {"fruits": ["apple", "pear", "melon"]}
print(json.dumps(obj_dict))
  • this got me to the idea, which worked perfectly fruits = ["apple", "pear", "melon"] return jsonify({"fruits": fruits}) An one-liner, thnx – kxell2001 Apr 13 '20 at 13:31