Sunday, 22 August 2021

RestApi json results into a csv using python

I'm trying to write the response returned from RestApi URL into a csv file for provided port number(Interactive mode) and selected users users_list.txt (Script mode). I have the below code to do the job.

import json
import csv
import urllib.request
import subprocess

portvalue = input("Please enter an Port Number:\n")
portvalue = int(portvalue)
print(f'You entered {portvalue}')

tooluser='admin'
toolpassword='password'

user = open('users-list.txt')
for line in user:
   bash_com = 'curl --user {tooluser}:{toolpassword} http://198.98.99.12:46567/{portvalue}/protects/{user}  \
         | jq --arg a_port {portvalue} --arg a_userid {user} 'map(.+{"userid":{user}}+{"port":{portvalue}})'' as url:

subprocess.Popen(bash_com)
output = subprocess.check_output(['bash','-c', bash_com])
print(line)
myfile.close()

# with urllib.request.urlopen("curl --user admin:password http://198.98.99.12:46567/{port}/protects/{user} | jq") as url:

data = json.loads(url.read().decode())

fname = "output.csv"

with open(fname, "w") as file:
    csv_file = csv.writer(file,lineterminator='\n')
 csv_file.writerow(["depotFile","host","isgroup","line","perm","user","port","userid"])
    for item in data["raw_data"]:
        csv_file.writerow([item['depotFile'],item['host'],item['isgroup'],item['line'],item['perm'],item['user'],item['port'],item['userid']])

One of the json output format as follows,

[
  {
    "depotFile": "//LIB/Include/...",
    "host": "*",
    "isgroup": "",
    "line": "19",
    "perm": "open",
    "user": "5G",
    "port": "2324",
    "userid": "sanchez.ricardo"
   },
....
......
.........
]

Expected output csv file:-

Sno     depotFile       host  isgroup  line   perm    user    port  userid
1   //LIB/Include/...   *              19     open    5G      2324  sanchez.ricardo
2   //LIB/...           *              19     write   6G      2324  varun.sharma
3   //AND/RIO/...       *              20     write   AND     2324  daniel.vel

I'm unable to process the RestApi URl in above code. Please help me to achieve this in python. Thanks in advance for your help.



from RestApi json results into a csv using python

No comments:

Post a Comment