Skip to content

Instantly share code, notes, and snippets.

@iameskild
Last active November 6, 2023 15:58
Show Gist options
  • Save iameskild/07b7578b16ba9d389659e086cc021274 to your computer and use it in GitHub Desktop.
Save iameskild/07b7578b16ba9d389659e086cc021274 to your computer and use it in GitHub Desktop.
import csv
import json
import uuid
import time
def update_json_from_csv(csv_file, json_file):
users = []
with open(csv_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
user = {
"id": str(uuid.uuid4()),
"createdTimestamp": int(time.time() * 1000),
"username": row["email"],
"enabled": True,
"totp": False,
"emailVerified": False,
"firstName": row["first_name"],
"lastName": row["last_name"],
"email": row["email"],
"disableableCredentialTypes": [],
"requiredActions": [
"UPDATE_PASSWORD"
],
"notBefore": 0,
"credentials": [
{
"type": "password",
"value": "123",
}
],
"access": {
"manageGroupMembership": True,
"view": True,
"mapRoles": True,
"impersonate": True,
"manage": True
},
"groups": [
"analyst"
]
}
users.append(user)
with open(json_file, 'w') as jsonfile:
json.dump({"users": users}, jsonfile, indent=4)
# Usage example
csv_file = "users.csv"
json_file = "output.json"
update_json_from_csv(csv_file, json_file)
@iameskild
Copy link
Author

@rsignell the important thing for github auth to keep in mind is that the user's github username will also be their username in keycloak. That means you can modify line 15 so that it pull row['github_username'] (if that column exists in your csv).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment