Skip to content

Instantly share code, notes, and snippets.

@MatthewPatience
Created March 12, 2020 20:28
Show Gist options
  • Save MatthewPatience/e04e63af07cf003d858089aa3e55a0c4 to your computer and use it in GitHub Desktop.
Save MatthewPatience/e04e63af07cf003d858089aa3e55a0c4 to your computer and use it in GitHub Desktop.
# This script will parse a json response from randomuser.me and convert it to the xml
# format required to import into Salesforce Demandware
#
# It fetches 500 random users from Canada. Just modify the URL parameters to modify these conditions.
import json
import os, sys
import codecs
from datetime import datetime
from xml.etree.ElementTree import ElementTree, Element, SubElement, Comment, tostring
from xml.etree import ElementTree
from xml.dom import minidom
import urllib.request, json
import random
import string
UTF8Writer = codecs.getwriter('utf8')
def randomPassword(stringLength=12):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
def prettify(elem):
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
with urllib.request.urlopen("https://randomuser.me/api/?format=json&nat=ca&exc=cell,registered&noinfo&results=500") as url:
data = json.loads(url.read().decode())
results = data['results']
currentId = 1
customersRoot = Element('customers', {'xmlns':'http://www.demandware.com/xml/impex/customer/2006-10-31'})
for customer in results:
customerElement = SubElement(customersRoot, 'customer', {'customer-no':str(currentId)})
credentialElement = SubElement(customerElement, 'credentials')
loginElement = SubElement(credentialElement, 'login')
loginElement.text = customer['email']
passwordElement = SubElement(credentialElement, 'password', {'encrypted': 'false'})
passwordElement.text = randomPassword()
loginEnabledElement = SubElement(credentialElement, 'enabled-flag')
loginEnabledElement.text = 'true'
pwQuestionElement = SubElement(credentialElement, 'password-question')
pwwAnswerElement = SubElement(credentialElement, 'password-answer')
profileElement = SubElement(customerElement, 'profile')
firstNameElement = SubElement(profileElement, 'first-name')
firstNameElement.text = customer['name']['first']
lastNameElement = SubElement(profileElement, 'last-name')
lastNameElement.text = customer['name']['last']
emailElement = SubElement(profileElement, 'email')
emailElement.text = customer['email']
phoneElement = SubElement(profileElement, 'phone-home')
phoneElement.text = customer['phone']
genderElement = SubElement(profileElement, 'gender')
if customer['gender'] == 'male':
genderElement.text = '1'
else:
genderElement.text = '2'
#
# Uncomment these fields if you want the customers to have a profile photo.
# This will require you to add a custom field to your SFCC Profile object.
#
# customAttributesElement = SubElement(profileElement, 'custom-attributes')
# photoElement = SubElement(customAttributesElement, 'custom-attribute', {'attribute-id':'photo'})
# photoElement.text = customer['picture']['large']
addressesElement = SubElement(customerElement, 'addresses')
addressElement = SubElement(addressesElement, 'address', {'address-id':'home', 'preferred':'true'})
firstNameAddressElement = SubElement(addressElement, 'first-name')
firstNameAddressElement.text = customer['name']['first']
lastNameAddressElement = SubElement(addressElement, 'last-name')
lastNameAddressElement.text = customer['name']['last']
address1Element = SubElement(addressElement, 'address1')
address1Element.text = str(customer['location']['street']['number']) + ' ' + customer['location']['street']['name']
cityElement = SubElement(addressElement, 'city')
cityElement.text = customer['location']['city']
postalElement = SubElement(addressElement, 'postal-code')
postalElement.text = customer['location']['postcode']
stateElement = SubElement(addressElement, 'state-code')
stateElement.text = customer['location']['state']
countryElement = SubElement(addressElement, 'country-code')
if customer['location']['country'] == 'United States':
countryElement.text = 'US'
else:
countryElement.text = 'CA'
currentId += 1
f = open("customers-import-{}.xml".format(datetime.now()), mode="w", encoding="utf-8")
f.write(prettify(customersRoot))
f.close()
@ravster
Copy link

ravster commented Mar 18, 2020

shell version

#!/bin/sh

# Get JSON from intarwebz
# curl "https://randomuser.me/api/?format=json&nat=ca&exc=cell,registered&noinfo&results=500" > a1.json

# Convert JSON to lines
jq -r '.results[] | [.email, .login.password, .name.first, .name.last, .phone, .gender,
  .location.street.number, .location.street.name, .location.city, .location.postcode,
  .location.state, .location.country] | @tsv' < a1.json |

# Convert lines to XML
awk '
function country_code(str) {
  if ( str == "United States" )
    return "USA"
  else
    return "CA"
}
function gender(str) {
  if ( str == "male")
    return 1
  else
    return 2
}
BEGIN {
  FS="\t"
  printf "<customers xmlns=\"http://www.demandware.com/xml/impex/customer/2006-10-31\">\n"
}
{
printf "<customer customer-no=\"%d\">\n", NR
printf "  <credentials>\n"
printf "    <login>%s</login>\n", $1
printf "    <password>%s</password>\n", $2
printf "  </credentials>\n"
printf "  <profile>\n"
printf "    <first-name>%s</first-name>\n", $3
printf "    <last-name>%s</last-name>\n", $4
printf "    <email>%s</email>\n", $1
printf "    <phone-home>%s</phone-home>\n", $5
printf "    <gender>%s</gender>\n", gender($6)
printf "  </profile>\n"
printf "  <addresses><address>\n"
printf "    <first-name>%s</first-name>\n", $3
printf "    <last-name>%s</last-name>\n", $4
printf "    <address1>%s %s</address1>\n", $7, $8
printf "    <city>%s</city>\n", $9
printf "    <postal-code>%s</postal-code>\n", $10
printf "    <state-code>%s</state-code>\n", $11
printf "    <country-code>%s</country-code>\n", country_code($12)
printf "  </address></addresses>\n"
printf "</customer>\n"
}
END {printf "</customers>\n"}
'

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