-3
import json

import requests

product_code = input("Enter the product code: ")

r = requests.get(f'https://api.fda.gov/device/510k.json?search=product_code:{product_code}')

packages_json = r.json()

packages = json.dumps(packages_json["results"], indent=2)

#display desired product code info
print(packages)

# https://api.fda.gov/device/510k.json?search=product_code:QBJ

My code currently just prints out the JSON file. I was hoping I could get some help or guidance on displaying the data in a visually pleasing format. I was thinking CSV to be similar to this site . But any other ideas would be appreciated. thank you

link to json

MAYANK SHARMA
  • 256
  • 1
  • 3
  • 10
Craig
  • 9
  • 3
  • Does this answer your question? [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – MAYANK SHARMA Jun 16 '21 at 08:37

2 Answers2

0

This question already has an answer here.

Although there are multiple ways to convert JSON to a CSV file, using the pandas library would be much easier.

The following commands will provide a solution to your problem:

  import pandas as pd
  df = pd.read_json()
  df.to_csv("Your File")

 
MAYANK SHARMA
  • 256
  • 1
  • 3
  • 10
  • If you think that a question has already been asked and answered, flag it as a duplicate, instead of repeating the answer. – mkrieger1 Jun 16 '21 at 08:33
0

you can do it using PANDAS library:

import pandas as pd
df = pd.read_json (r'Path where the JSON file is saved\File Name.json')
df.to_csv (r'Path where the new CSV file will be stored\New File Name.csv', index = None)

you can use TKINTER Package to show it visually:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getJSON ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_json (import_file_path)
    
browseButton_JSON = tk.Button(text="      Import JSON File     ", command=getJSON, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_JSON)

def convertToCSV ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
    read_file.to_csv (export_file_path, index = None, header=True)

saveAsButton_CSV = tk.Button(text='Convert JSON to CSV', command=convertToCSV, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton_CSV)

def exitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
     
exitButton = tk.Button (root, text='       Exit Application     ',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

output

additionally, you can check here

Abhishek Chhabra
  • 366
  • 2
  • 12