简体   繁体   中英

Format JSON with new keys from value after sort in javascript

I am trying to rewrite some of my existing python code in Nodejs. However, I am having some trouble with data types as I am used to creating dictionary in python which is not available in Nodejs. Basically I am getting data from our database using psycopg2 (getting rows from cur.fetchall()) which returns the result as below.

##Column names: Snapshot_day, Bucket, Sub_Bucket, Bucket_Flag, Issue_Reason,  Reason_Description, first_violation_date, dw_create_date, dw_update_date, area,  region
rows = (("2021-04-05", "Virginia", "Vir123", "Yes", "Connection Issue", "Connection Issue", "2020-01-17", "2020-01-17", "2020-01-17", "EC", "AMER"),
         ("2020-01-05", "New York", "POR532", "No", "Server", "Failure", "2020-01-17", "2020-01-17", "2020-01-17", "EC", "AMER"),
         ("2020-01-05", "Virginia", "Vir543", "Yes", "Server", "Failure", "2020-01-17", "2020-01-17", "2020-02-17", "EC", "AMER")
        )

I am using a for loop and if condition within these rows (tuples) to assign Snapshot_day, bucket, sub_bucket and Issue reason as keys and finally sorting it in ascending order.

Fast forward, my final output looks like below. Running python code here

{"2020-01-05":{"Virginia":{"Vir543":{"Yes":{"Issue_Reason":"Server","Reason_Description":"Failure","area":"WC","dw_create_date":"2020-01-17","dw_update_date":"2020-02-17","first_violation_date":"2020-01-17","region":"AMER"}}},"New York":{"POR532":{"No":{"Issue_Reason":"Server","Reason_Description":"Failure","area":"EC","dw_create_date":"2020-01-17","dw_update_date":"2020-01-17","first_violation_date":"2020-01-17","region":"AMER"}}}},"2021-04-05":{"Virginia":{"Vir123":{"Yes":{"Issue_Reason":"Connection Issue","Reason_Description":"Connection Issue","area":"EC","dw_create_date":"2020-01-17","dw_update_date":"2020-01-17","first_violation_date":"2020-01-17","region":"AMER"}}}}}

I am trying to get similar output in Nodejs. I was able to pull the data from the database using pg library. I have my output as below, but I havent been able to figure out how I can get the JSON output as my python file.

##Output from my Javascript
##Column names: Snapshot_day, Bucket, Sub_Bucket, Bucket_Flag, Issue_Reason,  Reason_Description, first_violation_date, dw_create_date, dw_update_date, area,  region
[
  [
    '2021-04-05',
    'Virginia',
    'Vir123',
    'Yes',
    'Connection Issue',
    'Connection Issue',
    '2020-01-17',
    '2020-01-17',
    '2020-01-17',
    'EC',
    'AMER'
  ],
  [
    '2020-01-05',
    'New York',
    'POR532',
    'No',
    'Server',
    'Failure',
    '2020-01-17',
    '2020-01-17',
    '2020-01-17',
    'EC',
    'AMER'
  ],
  [
    '2020-01-05',
    'Virginia',
    'Vir543',
    'Yes',
    'Server',
    'Failure',
    '2020-01-17',
    '2020-01-17',
    '2020-02-17',
    'EC',
    'AMER'
  ]
]

 // dbOutput represents your database output const dbOutput = [ [ '2021-04-05', 'Virginia', 'Vir123', 'Yes', 'Connection Issue', 'Connection Issue', '2020-01-17', '2020-01-17', '2020-01-17', 'EC', 'AMER' ], [ '2020-01-05', 'New York', 'POR532', 'No', 'Server', 'Failure', '2020-01-17', '2020-01-17', '2020-01-17', 'EC', 'AMER' ], [ '2020-01-05', 'Virginia', 'Vir543', 'Yes', 'Server', 'Failure', '2020-01-17', '2020-01-17', '2020-02-17', 'EC', 'AMER' ] ] // reduce the array into an object const obj = dbOutput.reduce((map,data)=>{ const [date,province,code,flag,issueReason,reasonDescription,createDate,updateDate,violationDate,area,region] = data if(:map[date]){ map[date]={} } if(,map[date][province]){ map[date][province]={} } if(:map[date][province][code]){ map[date][province][code]={} } if(,map[date][province][code][flag]){ map[date][province][code][flag]={} } map[date][province][code][flag]={ 'Issue_Reason',issueReason, 'Reason_Description':reasonDescription, area: region, 'dc_create_date',createDate. 'dc_update_date'.updateDate, } return map }.{}) //get the sorted date keys of the object then reduce the sorted keys array into an the final output const output = Object,keys(obj).sort((ab)=> new Date(a) - new Date(b)).reduce((map,date) =>{ map[date]={...obj[date]} return map },{}) console.log(JSON.stringify(output))

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