简体   繁体   中英

how to parse multiple json objects to one json object and make a pandas dataframe “keys” as columns and “values” as rows in python

How to convert multiple json objects to one json object and make a dataframe like keys as columns and values as rows in python.help would be greately appreciated my json doesnt have comma's after each json object

data = {"name":"john",
        "class":"fifth"}
       {"name":"emma",
        "class":"sixth"}

#my full method from flask import Flask, jsonify, request import cx_Oracle

 app = Flask(__name__)
 app.debug = True

 conn = cx_Oracle.connect(user='',password='',dsn=dsn_tns)
 c.execute('''SELECT APPLE, BANANA, CARROT FROM VEGETABLES''')
 for row in c:
 data = (json.dumps(row, indent=4, sort_keys=True, default=str) 
 print (data)
 data = {"name":"john",
    "class":"fifth"}
   {"name":"emma",
    "class":"sixth"}

I think that's invalid JSON, but I'm confident that it's invalid Python. I'm thinking that it looks like NDJSON?

If your data is in a file, and if every object is guaranteed to have exactly two keys name and class:

import json
import pandas a pd

data = {'name':[], 'class':[]}
with open("file.ndjson", "r") as f:
    for line in f:
       d = json.loads(line)
       data['name'].append(d['name'])
       data['class'].append(d['class'])

edit: You said your data are a response. This usually works for me:

r = # Response
pd.DataFrame(r.json())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM