-1

How to save this json response to csv with columns CVE | RISKMETERScore | RiskMeterHistory

{
  "CVE-2022-22721": {
    "id": 9864403,
    "risk_meter_score": 33,
    "risk_meter_score_history": [
      {
        "changed_at": "2022-03-15T04:05:32.000Z",
        "from": 25,
        "to": 34
      },
      {
        "changed_at": "2022-03-19T05:15:12.000Z",
        "from": 34,
        "to": 30
      },
      {
        "changed_at": "2022-06-29T04:34:45.000Z",
        "from": 30,
        "to": 33
      }
    ]
  }
}
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • 4
    How it should look in a csv file? – funnydman Jul 10 '22 at 19:09
  • Column names CVE | RISKMETERScore | RiskMeterHistory CVE-2022-22721 | 33 | changed_at": "2022-03-15T04:05:32.000Z" "from": 25, "to": 34 CVE-2022-22721 | 33 | changed_at": "2022-03-19T05:15:12.000Z" "from": 34, "to": 30 - these would be the columns and it would have the data according – Rahul Rakshit Jul 11 '22 at 04:51

1 Answers1

0

Using pandas, you can try this out:

#!/usr/bin/python3

import pandas as pd

pdObj = pd.read_json('cve.json', orient='index')
pdObj.to_csv('cve.csv', index=False)

firstly, prepare the JSON file. Then, use the read_json() function to convert it into a Pandas object. In the last line, you need to use the Pandas to_csv() function to convert Pandas object to CSV data or export it into a file.

checkout this stackoverflow question

In the above code, I have passed orient=’index’.

Orient is an indication of the expected JSON string format. Compatible JSON strings can be produced by to_json() with a corresponding orient value. The set of possible orients is:

‘split‘: dict like {index -> [index], columns ->
, data -> [values]}
‘records‘: list like [{column -> value}, … , {column -> value}]
‘index‘: dict like {index -> {column -> value}}
‘columns‘: dict like {column -> {index -> value}}
‘values‘: just the values array

You can go through the above options to achieve your desired result.

Sten Techy
  • 79
  • 1
  • 7
  • If I try to save the Json i get - TypeError: Object of type Response is not JSON serializable – Rahul Rakshit Jul 11 '22 at 04:51
  • @RahulRakshit which of the orient option did you use, when I run use orient=index, I don't get any error and the result(cve.csv) I get is: ---------------------------------------------------------------------- `id,risk_meter_score,risk_meter_score_history 9864403,33,"[{'changed_at': '2022-03-15T04:05:32.000Z', 'from': 25, 'to': 34}, {'changed_at': '2022-03-19T05:15:12.000Z', 'from': 34, 'to': 30}, {'changed_at': '2022-06-29T04:34:45.000Z', 'from': 30, 'to': 33}]"` – Sten Techy Jul 12 '22 at 10:58