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
  • 110,080
  • 24
  • 357
  • 317
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
  • 110,080
  • 24
  • 357
  • 317
Charles Merriam
  • 18,628
  • 6
  • 71
  • 79