Last active
March 27, 2024 19:23
-
-
Save markgarrigan/ccc68552cabee1c433d0c78cd32c2666 to your computer and use it in GitHub Desktop.
LDAP LDIF to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if an LDIF file was provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <ldif_file>" | |
exit 1 | |
fi | |
ldif_file=$1 | |
# Begin the JSON array | |
echo "[" | |
# Initialize isFirst to handle commas between objects | |
isFirst=true | |
# Process the LDIF file | |
awk 'BEGIN {FS=": "; OFS=": "} | |
{ | |
if ($0 ~ /^dn:/) { | |
if (!isFirst) { | |
print " }," | |
} else { | |
isFirst=false | |
} | |
print " {" | |
print " \"$dn\": \"" $2 "\"" | |
} else if ($0 ~ /^#/) { | |
# Skip comment lines | |
next | |
} else if ($0 ~ /^[a-zA-Z]/) { | |
print " \"" $1 "\": \"" $2 "\"" | |
} | |
} END { | |
print " }" | |
}' "$ldif_file" | |
# End the JSON array | |
echo "]" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
convert_to_json() { | |
local input="$1" | |
local -A keys_seen | |
local json="{" | |
local first_entry=true | |
# Convert input into lines, one per key-value pair | |
IFS=' ' read -ra kv_pairs <<< "$input" | |
for kv in "${kv_pairs[@]}"; do | |
IFS=: read key value <<< "$kv" | |
# Remove potential whitespace around keys/values | |
key=$(echo "$key" | xargs) | |
value=$(echo "$value" | xargs) | |
# Skip empty keys | |
[[ -z "$key" ]] && continue | |
# Check if key has been seen before | |
if [[ -n "${keys_seen[$key]}" ]]; then | |
# Append value to existing key | |
new_value=$(echo "$json" | sed "s/\"$key\": \[\(.*\)\]/\"$key\": [\1, \"$value\"]/") | |
json="$new_value" | |
else | |
# Add new key-value pair | |
if [ "$first_entry" = true ]; then | |
first_entry=false | |
else | |
json+=", " | |
fi | |
json+="\"$key\": [\"$value\"]" | |
keys_seen[$key]=1 | |
fi | |
done | |
json+="}" | |
echo "$json" | |
} | |
# Example usage | |
input="dn: anything,canbehere mail: [email protected] dn: somethingelse" | |
convert_to_json "$input" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
convert_to_json() { | |
local input="$1" | |
local -A kv_pairs | |
# Split input into an array of 'key: value' strings | |
IFS=' ' read -ra pairs <<< "$input" | |
for pair in "${pairs[@]}"; do | |
IFS=: read -r key value <<< "$pair" | |
# Trim whitespace from key and value | |
key=$(echo "$key" | xargs) | |
value=$(echo "$value" | xargs) | |
# Handle duplicate keys by appending values to an array | |
if [[ -n "${kv_pairs[$key]}" ]]; then | |
# If already an array, add to it; otherwise, create an array | |
if [[ "${kv_pairs[$key]}" =~ ^\[\ \".*\"\ \]$ ]]; then | |
kv_pairs[$key]="${kv_pairs[$key]%]*}, \"$value\"]" | |
else | |
kv_pairs[$key]="[ \"${kv_pairs[$key]}\", \"$value\" ]" | |
fi | |
else | |
kv_pairs[$key]="\"$value\"" | |
fi | |
done | |
# Construct JSON string | |
local json="{" | |
local sep="" | |
for key in "${!kv_pairs[@]}"; do | |
json+="$sep\"$key\": ${kv_pairs[$key]}" | |
sep=", " | |
done | |
json+="}" | |
echo "$json" | |
} | |
# Example usage | |
input="dn: anything,canbehere mail: [email protected] dn: somethingelse" | |
convert_to_json "$input" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
convert_to_json() { | |
local input_str="$1" | |
local -A kv_pairs # Declare associative array for storing key-value pairs | |
# Normalize and parse the input string into key-value pairs | |
echo "$input_str" | tr ' ' '\n' | while IFS=: read -r key value; do | |
# Trim spaces from key and value | |
key=$(echo "$key" | xargs) | |
value=$(echo "$value" | xargs) | |
# Check if key exists and handle multiple values | |
if [[ -n "${kv_pairs[$key]}" ]]; then | |
kv_pairs["$key"]="${kv_pairs[$key]}, \"$value\"" | |
else | |
kv_pairs["$key"]="\"$value\"" | |
fi | |
done | |
# Construct JSON string | |
local json="{" | |
local first=true | |
for key in "${!kv_pairs[@]}"; do | |
if [ "$first" = true ]; then | |
first=false | |
else | |
json+=", " | |
fi | |
# Check if we have multiple values for this key | |
if [[ ${kv_pairs[$key]} =~ , ]]; then | |
json+="\"$key\": [${kv_pairs[$key]}]" | |
else | |
json+="\"$key\": ${kv_pairs[$key]}" | |
fi | |
done | |
json+="}" | |
echo "$json" | |
} | |
# Example usage | |
input="dn: anything,canbehere mail: [email protected] dn: somethingelse" | |
convert_to_json "$input" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
convert_to_json() { | |
local input_str="$1" | |
declare -A kv_pairs # Associative array to hold key-value pairs | |
# Normalize input and read each key-value pair | |
# Replace ' key:' with newline to ensure each key-value pair is on a new line | |
local formatted_input=$(echo "$input_str" | sed 's/ \([^ ]\+\):/\n\1:/g') | |
while IFS=: read -r key value; do | |
# Trim leading and trailing spaces from key and value | |
key=$(echo "$key" | xargs) | |
value=$(echo "$value" | xargs) | |
# Append value to existing key, using a unique delimiter for multiple values | |
if [[ -n "${kv_pairs[$key]}" ]]; then | |
# If key exists, append value with delimiter | |
kv_pairs["$key"]+=$'\4'"$value" # Using ASCII EOT (End of Transmission) as a delimiter | |
else | |
# If key does not exist, add it | |
kv_pairs["$key"]="$value" | |
fi | |
done <<< "$formatted_input" | |
# Begin JSON output | |
echo -n '{' | |
local first=1 | |
for key in "${!kv_pairs[@]}"; do | |
if [[ $first -eq 0 ]]; then | |
echo -n ', ' | |
else | |
first=0 | |
fi | |
# Check if the key has multiple values (indicated by the delimiter) | |
if [[ "${kv_pairs[$key]}" =~ $'\4' ]]; then | |
# Replace the delimiter with ", " to format as a JSON array | |
local joined_values=$(echo "${kv_pairs[$key]}" | sed "s/$'\4'/\", \"/g") | |
echo -n "\"$key\": [\"$joined_values\"]" | |
else | |
# Single value, just output it | |
echo -n "\"$key\": \"${kv_pairs[$key]}\"" | |
fi | |
done | |
echo '}' | |
} | |
# Example usage | |
input="dn: anything,canbehere mail: [email protected] dn: somethingelse" | |
convert_to_json "$input" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
convert_to_json() { | |
local input="$1" | |
# Use awk to process the input string, split by ' ' and ':' | |
# It handles both unique and duplicate keys, assembling arrays for duplicates | |
echo "$input" | awk -v RS=" " ' | |
{ | |
# Split each record by ":", key is $1, value is the rest | |
split($0, parts, /:/, seps); | |
key = parts[1]; | |
value = parts[2]; | |
for(i = 3; i in parts; i++) { | |
value = value ":" parts[i]; | |
} | |
# Remove leading and trailing whitespace | |
gsub(/^ +| +$/, "", key); | |
gsub(/^ +| +$/, "", value); | |
# Process key-value pairs | |
if (length(key) > 0) { | |
if (key in kv) { | |
# Key exists; append value with a separator | |
kv[key] = kv[key] "‚" value; # Use a rare character as a separator | |
} else { | |
# New key | |
kv[key] = value; | |
} | |
} | |
} | |
END { | |
# Begin JSON output | |
printf "{"; | |
for (k in kv) { | |
# Split values by the separator, if more than one exists | |
n = split(kv[k], vals, "‚"); | |
if (n > 1) { | |
# Output as an array | |
printf "\"%s\":[", k; | |
for (v=1; v <= n; v++) { | |
printf "\"%s\"", vals[v]; | |
if (v < n) printf ", "; | |
} | |
printf "]"; | |
} else { | |
# Output as a single value | |
printf "\"%s\":\"%s\"", k, kv[k]; | |
} | |
if (--c > 0) printf ", "; | |
} | |
printf "}\n"; | |
}' c=$(echo "$input" | awk '{print gsub(/ /," ")}') # Count of total keys for comma handling | |
} | |
# Example usage | |
input="dn: anything,canbehere mail: [email protected] dn: somethingelse" | |
convert_to_json "$input" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment