Reading JSON Format with Python

By | January 15, 2021
254 Views

Sinopsis

Script yang saya buat sebenarnya mempunyai tugas yang sangat sederhana sekali yaitu membaca format JSON dari sebuah web service, karena data tersebut digunakan untuk melakukan analisis terdapat penjualan harian sebuah toko. Berikut merupakan keluaran JSON tersebut.

[{"label":"12-04-17","y":1800000},{"label":"14-04-17","y":1800000},{"label":"15-04-17","y":180000},{"label":"16-04-17","y":180000},{"label":"17-04-17","y":1800000},{"label":"18-04-17","y":1700000},{"label":"19-04-17","y":1800000},{"label":"20-04-17","y":1800000},{"label":"21-04-17","y":1700000},{"label":"22-04-17","y":1800000},{"label":"23-04-17","y":1800000},{"label":"24-04-17","y":1800000},{"label":"25-04-17","y":1700000},{"label":"26-04-17","y":1800000},{"label":"27-04-17","y":1800000},{"label":"28-04-17","y":1800000},{"label":"29-04-17","y":1800000},{"label":"30-04-17","y":1800000},{"label":"01-05-17","y":1800000},{"label":"02-05-17","y":1800000},{"label":"03-05-17","y":1800000},{"label":"04-05-17","y":1800000},{"label":"05-05-17","y":1800000},{"label":"06-05-17","y":1800000},{"label":"07-05-17","y":1800000},{"label":"08-05-17","y":1800000},{"label":"09-05-17","y":180000},{"label":"10-05-17","y":1800000},{"label":"11-05-17","y":188888},{"label":"12-05-17","y":1800000},{"label":"13-05-17","y":1800000},{"label":"14-05-17","y":1800000},{"label":"15-05-17","y":1800000},{"label":"16-05-17","y":1800000},{"label":"17-05-17","y":188888},{"label":"18-05-17","y":188888},{"label":"19-05-17","y":188888},{"label":"20-05-17","y":188888},{"label":"21-05-17","y":188888},{"label":"22-05-17","y":1800000},{"label":"01-06-17","y":1300000},{"label":"02-06-17","y":1500000},{"label":"03-06-17","y":1500000},{"label":"04-06-17","y":1500000},{"label":"05-06-17","y":1400000},{"label":"06-06-17","y":1250000},{"label":"07-06-17","y":1300000},{"label":"08-06-17","y":1200000},{"label":"09-06-17","y":1200000},{"label":"10-06-17","y":1250000},{"label":"11-06-17","y":1300000},{"label":"12-06-17","y":1200000},{"label":"13-06-17","y":1800000},{"label":"14-06-17","y":1800000},{"label":"15-06-17","y":1800000},{"label":"16-06-17","y":1800000},{"label":"17-06-17","y":1800000},{"label":"18-06-17","y":1800000},{"label":"19-06-17","y":1200000},{"label":"20-06-17","y":1200000}]

Terdapat id yaitu y dan label, dimana y merupakan omzet dalam rupiah dan label merupakan tanggal transaksi harian. Kemudian kita akan menggunakan Python dan sedikit grafik untuk melihat trend penjualannya, tentu kita akan menggunakan beberapa library seperti urllib, matplotlib, numpy, dan json. Karena hal yang mudah membuat web service cukup menggunakan PHP dengan framework codeigniter. Menggunakan library json, urlib untuk membaca data dari protokol HTTP

import urllib
import json
from matplotlib import pyplot as plt
import numpy as np

def ritelDataTransaksi():
    url = "http://localhost/pospay/index.php/transaksi_ritel/json_data_transaksi_30_terakhir"
    f = None
    try:    
        f = urllib.urlopen(url)        
    except:
        print 'error koneksi'
    if f!=None:
        myfile = f.read()        
        d = json.loads(myfile)        
        x = list()
        y = list()
        for i in range(0,len(d)):            
            x.append(d[i]['y'])
            y.append(d[i]['label'])
        x = np.array(x)
        fig = plt.figure(1)
        ax = fig.add_subplot(111)
        plt.plot(x)        
        ax.set_xticklabels(y, rotation = 45)    
        ax.axis(ymin=0,ymax=2000000)
        plt.title('Omzet Harian')
        plt.show()
    else :
        print 'error koneksi'

ritelDataTransaksi()
k = input('ok')

Leave a Reply

Your email address will not be published.




Enter Captcha Here :