Keithx
Keithx

Reputation: 3158

Transforming JSON string to Pandas DataFrame in Flask

I'm parsing JSON data in Flask from POST request. Everything seems to be fine and works ok:

from flask import Flask
from flask import request
import io
import json
import pandas as pd

app = Flask(__name__)

@app.route('/postjson', methods = ['POST'])

def postJsonHandler():

    print (request.is_json)
    content = request.get_json()
    df = pd.io.json.json_normalize(content)
    print (df)
    return 'JSON posted'

app.run(host='0.0.0.0', port= 8090)

The output looks like this:

True 

          columns                                               data
0  [Days, Orders]  [[10/1/16, 284], [10/2/16, 633], [10/3/16, 532...

Then I try to transform json to pandas dataframe using json_normalize() function. So I receiving the result close to pandas dataframe but it is not yet it. What changes in the code should I do to receive classical Pandas Dataframe format with columns and data inside.

Thanks in advance.

Upvotes: 1

Views: 2465

Answers (1)

Keithx
Keithx

Reputation: 3158

Solved the problem. The idea was to use parameters of the json_normalize() function something like that:

df = pd.io.json.json_normalize(content, 'data')

Upvotes: 1

Related Questions