Felwah
Felwah

Reputation: 65

Create Json object using flask

I am implementing a python API using flask framework, here is my code:

    current_months = [this_month_list]
    result = pd.concat(current_months)
    my_array = np.array(result['city'])
    freqs = Counter(my_array)
    return jsonify(freqs)

My problem is in creating the JSON object. By the above code the JSON is like:

{
'Riyadh': 50
'Jeddah': 10
'Los Angeles': 30}

However, I want to include a message to the JSON object. This is the result that I want to achieve:

{
'Message' : "I want to include this message"
'Result' : {
    'Riyadh': 50
    'Jeddah': 10
    'Los Angeles': 30
}

}

Upvotes: 2

Views: 4387

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599450

So just wrap your data:

data = {
    'Message' : "I want to include this message"
    'Result' : freqs }
return jsonify(data)

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19634

Just append them to a dictionary:

freqs = {
    'Message': 'Some msg',
    'Result': Counter(my_array)
}
return jsonify(freqs)

Upvotes: 3

Related Questions