Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Created July 5, 2017 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Xophmeister/33e6abe5e055e122f5093f1acbc91793 to your computer and use it in GitHub Desktop.
Save Xophmeister/33e6abe5e055e122f5093f1acbc91793 to your computer and use it in GitHub Desktop.
Quick-and-Dirty LDIF to JSON convertor
#!/usr/bin/env awk -f
# Convert LDIF into JSON
# MIT License
# Copyright (c) 2017 Christopher Harrison
function json_string(str) {
# Convert a string into an escaped JSON string, with enclosing quotes
return "\"" gensub(/"/, "\\\\\"", "g", str) "\""
}
BEGIN {
FS = "::? "
ORS = ""
in_dn = 0
first_dn = 0
# Array of results
print "["
}
/^#/ || $1 == "version" || $1 == "search" || $1 == "result" {
# Skip comments, version and ldapsearch result entities
next
}
$1 == "dn" {
# We've found an entity
if (first_dn) print ","
print "{"
in_dn = 1
first_dn = 1
# Reset attributes
delete attributes
}
in_dn {
if (NF == 2) {
# New attribute
last_attr = $1
attr_index = 1
if (last_attr in attributes) attr_index = length(attributes[last_attr]) + 1
value = $2
} else {
# Continuation of previous attribute
attr_index = length(attributes[last_attr])
value = attributes[last_attr][attr_index] gensub(/^ /, "", 1)
}
attributes[last_attr][attr_index] = value
}
in_dn && /^$/ {
# Write attributes
first_attr = 0
for (attr in attributes) {
if (first_attr) print ","
print json_string(attr) ":"
first_attr = 1
if (length(attributes[attr]) == 1) {
# Scalar attribute
print json_string(attributes[attr][1])
} else {
# Array attribute
print "["
first_arr = 0
for (i in attributes[attr]) {
if (first_arr) print ","
print json_string(attributes[attr][i])
first_arr = 1
}
print "]"
}
}
# End of entity
print "}"
in_dn = 0
}
END {
# End of array
print "]"
}
@mludvig
Copy link

mludvig commented Mar 7, 2019

Very useful, just what I needed.

Consider escaping backslashes in the output, I've got a lot of them in my Active Directory LDIF results and this change was required to make the JSON valid.

function json_string(str) {
  # Convert backslashes to double-backslashes
  str = gensub("\\\\", "\\\\\\\\", "g", str)
  # Convert a string into an escaped JSON string, with enclosing quotes
  return "\"" gensub(/"/, "\\\\\"", "g", str) "\""
}

Thanks for sharing your script!

@mohankumaru
Copy link

How do you run this code

@ericsopa
Copy link

ericsopa commented Jun 7, 2019

This doesn't work on Mac or Amazon Linux.

Linux

=====

$ awk --version
GNU Awk 3.1.7

Linux errors:

awk: ./ldif2json.awk:52:     value = attributes[last_attr][attr_index] gensub(/^ /, "", 1)
awk: ./ldif2json.awk:52:                                  ^ syntax error
awk: ./ldif2json.awk:55:   attributes[last_attr][attr_index] = value
awk: ./ldif2json.awk:55:                        ^ syntax error
awk: ./ldif2json.awk:68:       print json_string(attributes[attr][1])
awk: ./ldif2json.awk:68:                                         ^ syntax error
awk: ./ldif2json.awk:75:       for (i in attributes[attr]) {
awk: ./ldif2json.awk:75:                           ^ syntax error
awk: ./ldif2json.awk:77:         print json_string(attributes[attr][i])
awk: ./ldif2json.awk:77:                                           ^ syntax error

Mac

====

$ awk --version
awk version 20070501

Mac errors:

awk: syntax error at source line 52 source file ./ldif2json.awk
 context is
	    value = >>>  attributes[last_attr][ <<< attr_index] gensub(/^ /, "", 1)
awk: illegal statement at source line 53 source file ./ldif2json.awk
awk: syntax error at source line 55 source file ./ldif2json.awk

@Xophmeister
Copy link
Author

Xophmeister commented Jun 9, 2019

@ericsopa IIRC, you need GNU Awk 4, or later. Also note that the shebang will need to be changed in Linux, as it only allows one argument.

@Xophmeister
Copy link
Author

@mohankumaru Save the script somewhere in your path and make it executable, then pipe the contents of ldapsearch into it. For example:

ldapsearch '(uid=mohankumaru)' | ldif2json

@jhult
Copy link

jhult commented Apr 14, 2023

If you bump into issues with awk syntax, try installing gawk.

More details on my fork.

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