简体   繁体   中英

python: json.dumps output in custom format

I have a table info_tbl in postgresql

        Column        |          Type          | Modifiers 
----------------------+------------------------+-----------
 task_info            | character varying(100) | 
 timestamp            | date                   | 
 task_count           | integer                | 

So I basically do fetch operation on the db "select * from info_tbl" and get the data output in json format using json.dumps. But the out put i get is something like this:

[
  {
    "task_info": "ABC",
    "timestamp": "2017-04-30",
    "task_count": 993
  },
  {
    "task_info": "PQR",
    "timestamp": "2017-05-31",
    "task_count": 413
  }
]

While what i actually desire to achieve is something like this:

[
  {
    "task_info": "ABC",
    "data_to_plot": [["2017-04-30", "993"],["2017-05-28", "624"],["2017-06-21", "811"]]
  },
  {
    "task_info": "PQR",
    "data_to_plot": [["2017-05-31","413"],["2017-06-16", "773"],["2017-07-21", "941"],["2017-08-30", "493"]]
  }
]

these outputs are just for sake of understanding hence have presented only first two records while the actual table has more than 1000s of records. I'll be using these to plot charts.

turns out that @furas was right its not really a good practice to reformat a dump and hence used pandas to do the re-formatting. i thought it'll be useful for others who are also trying to prepare data as input for HighCharts plotting, henceforth, posting the work-around i did using pandas.

import psycopg2
from psycopg2.extras import RealDictCursor

con = psycopg2.connect("dbname='yourDBname' user='yourUserName' host='yourAddressOrLocalhost' password='yourPassword'")
cur = con.cursor(cursor_factory=RealDictCursor)

query = "select * from info_tbl"
cur.execute(query)

df = pd.DataFrame(cur.fetchall(),index=None)
df['data_to_plot'] = df.apply(lambda row: [str(row['timestamp']) , row['task_count']], axis = 1)

result = df.groupby('task_info')['data_to_plot'].apply(list).reset_index().to_json(orient='records')
#result
print(json.dumps(json.loads(result),indent=2))

output:

'''
    [
      {
        "task_info": "ABC",
        "data_to_plot": [["2017-04-30", "993"],["2017-05-28", "624"],["2017-06-21", "811"]]
      },
      {
        "task_info": "PQR",
        "data_to_plot": [["2017-05-31","413"],["2017-06-16", "773"],["2017-07-21", "941"],["2017-08-30", "493"]]
      }
    ]
'''

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