Get JSM Customer With Python

Read all customers from Jira Service Management and store the response in Microsoft Excel. In this example we utilize the great Data Science Tool Python Pandas.

#!/usr/bin/env python3
# File:
# Example: ./ -u -p wsafAewcfAfarfacrthejt42 --url --portalid 1

import requests
from requests.auth import HTTPBasicAuth
import json
import sys
import pandas as pd
import csv
import argparse

def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('-u', help='username like', required=True)
    arg_parser.add_argument('-p', help='password like a personal access token', required=True)
    arg_parser.add_argument('--url', help='like', required=True)
    arg_parser.add_argument('--portalid', help='e.g. displayed in the browser\'s url field when entering a Service Management portal', required=True)
    arg_parser.add_argument('-o', help='Microsoft Excel output filename', required=True)
    args = arg_parser.parse_args()

    output_structure = {}
    output_structure['data'] = []
    account_ids = []

    ### Get all organizations

    organizations = requests.get(
        url = args.url + '/rest/servicedeskapi/organization',
        headers = {"Content-type":"application/json"},
        auth = HTTPBasicAuth(args.u, args.p),
        params = {"limit":100})
    organizations = organizations.json()

    ### Get all customers of any organizations

    for organization in organizations['values']:
        customers = requests.get(
            url = args.url + '/rest/servicedeskapi/organization/' + organization['id'] + '/user',
            headers = {"Content-type":"application/json"},
            auth = HTTPBasicAuth(args.u, args.p),
            params = {"limit":1000})
        customers = customers.json()
        account_ids += [x['accountId'] for x in customers['values']]
        organization['customers'] = {'values': customers['values']}

    ### Get all not organized customers

    customers = requests.get(
        url = args.url + '/rest/servicedeskapi/servicedesk/' + args.portalid + '/customer',
        headers = {"Content-type": "application/json", "X-ExperimentalApi": "opt-in"},
        auth = HTTPBasicAuth(args.u, args.p),
        params = {"limit": 5000})
    customers = customers.json()
        "id": "",
        "name": "",
        "customers": {'values': [x for x in customers['values'] if x['accountId'] not in account_ids]}})

    ### Output

    filter_fields = ["id","name","accountId","emailAddress","displayName"]
    df = pd.json_normalize(output_structure, record_path=['data'])
    df = df.apply(lambda x: x.explode()).reset_index(drop=True)
    df['customers.values'].fillna({x: {} for x in df.index}, inplace=True)
    df_cv = pd.json_normalize(df['customers.values'], sep='_', errors='ignore')
    df = pd.concat([df] + [df_cv], axis=1).drop(columns=['customers.values'])[filter_fields]

    df.to_excel(args.o, index=False)

if __name__ == "__main__":

Reach out to me for professional support!