Home > Mobile >  pandas json object read error when using flask request get_json
pandas json object read error when using flask request get_json

Time:07-21

I am passing a JSON object through a POST API into a flask app. The goal is to convert it to a single row pandas DF and pass it on for further processing.

the JSON payload is as follows:

{
  "ABC": "123",
  "DATE": "2020-01-01",
  "AMOUNT": "100",
  "IDENTIFIER": "12345"
} 

The output of data=flask.request.get_json() and print(data) is

{'ABC': '123', 'DATE': '2020-01-01', 'AMOUNT': '100','IDENTIFIER': '12345'} 

But when I do a pd.read_json(data) on it I get an error ValueError: Invalid file path or buffer object type: <class 'dict'> Any ideas on how to handle this? I need the output to be

ABC    DATE        AMOUNT    IDENTIFIER
123   2020-01-01   100        12345

Thanks!

CodePudding user response:

Try this:

import pandas as pd

df = pd.DataFrame([data.values()], columns=data.keys())
print(df)

Output:

     ABC       DATE          AMOUNT   IDENTIFIER
 0   123       2020-01-01    100      12345
  • Related