0

I want to convert this dictionary to expected output in json format

sample_dict = {"app":"Apps","dev":"Dev-Func","test":"Testing"}

output = 
{"name":[
    {"option":"app", "value":"Apps"},
    {"option":"dev", "value":"Dev-Func"},
    {"option":"test", "value":"Testing"}
    ]
}
davidism
  • 121,510
  • 29
  • 395
  • 339
Prabhu S
  • 15
  • 6

1 Answers1

0

Use a list comprehension over the dict items to convert to the format you want. Then use Flask's jsonify function to return a JSON response.

from flask import jsonify

output = {'name': [ {'option': k, 'value': v} for (k, v) in sample_dict.items()]}
return jsonify(output)
davidism
  • 121,510
  • 29
  • 395
  • 339
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83