简体   繁体   中英

Generate JSON file for SAS dataset using Python

My Input Data is SAS dataset, Sample data

ID                             b'401200262'
    POLICYNO                             746346
    BIRTH                   1960-09-23 00:00:00
    HOMEKIDS                                  0
    YOJ                                     NaN
    INCOME                                  NaN
    PARENT1                               b'No'
    HOME_VAL                             252896
    MSTATUS                              b'Yes'
    GENDER                                 b'M'
    EDUCATION                      b'Bachelors'
    NPOLICY                                   2
    URBANICITY           b'Highly Urban/ Urban'
    AGE_BAND                         b'51 - 65'
    CREDIT_SCORE_BAND                  b'750 +'
    KIDSDRIV                                  0
    INITDATE                1988-05-12 00:00:00
    PLCYDATE                2001-04-07 00:00:00
    OLDCLAIM                               1554
    CLM_FREQ                                  5
    REVOKED                               b'No'
    MVR_PTS                                   1
    CLM_AMT                                   0
    CLM_DATE                                NaT
    CLAIM_FLAG                                0
    STATE                                 b'IL'
    TRAVTIME                             14.472
    CAR_USE                       b'Commercial'
    BLUEBOOK                              28560
    CAR_TYPE                     b'Panel Truck'
    RED_CAR                              b'yes'
    CAR_AGE                                  10
    TIF                                     104
    OCCUPATION                  b'Professional'
    PLCYYEAR                               2001
    CLM_EXPOSURE                              1
    LN_CLM_EXPOSURE                           0
    CUST_LOYALTY                        12.9024
    BLUEBOOK_1000                         28.56

I am trying to generate REST API JSON format for a particular record in this below example I have chosen 2nd row:

    import pandas as pd
    sasdt = pd.read_sas("c:\hmeq.sas7bdat")
    dfs = [] # holds data chunks
    dfs2=sasdt.loc[2]
    for chunk in sasdt:
        print('{"name":"',chunk,'_","value":',)

I am unable to generate my desired output something like this: Could you please help me in this

期望输出

Try this following:

import pandas as pd
import numpy
import json

sasdt = pd.read_sas('airline.sas7bdat')
dfs2 = sasdt.loc[2]

# avoid json.dumps raise "TypeError: Object of type 'int64' is not JSON serializable"
dfs2_values = [int(i) if type(i) == numpy.int64 else i for i in dfs2]

inputs = pd.DataFrame({'name':dfs2.index.to_list(), 'value':dfs2_values}).to_dict(orient='records')
json.dumps({'inputs':inputs})

You will get output like this:

'{"inputs": [{"name": "YEAR", "value": 1950.0}, {"name": "Y", "value": 1.569000005722046}, {"name": "W", "value": 0.27799999713897705}, {"name": "R", "value": 0.3156999945640564}, {"name": "L", "value": 1.3880000114440918}, {"name": "K", "value": 0.5730000138282776}]}'

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