python Object of type 'Attribute' is not JSON serializable

Question:

I am beginner in python, I am trying to write a python script to convert the ldap query result to json object.
After I get the datadump to the variable c, I attempt to format the string that I will use to convert to json object. But I get an error about "Object of type ‘Attribute’ is not JSON serializable"

any help will be appreciated.

 for field in c.entries:
     GivenName=field.GivenName
     sn = field.sn
     UserPrincipalName = field.userPrincipalName
     dumpData={"GivenName": GivenName, "sn": sn, "UserPrincipalName": UserPrincipalName}
     jsondata.append(dumpData)

Converting to json format I get the following error.

jsondatadump=json.dumps(jsondata)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib64/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Attribute' is not JSON serializable

I added another field to include memberOf field

{
            "GivenName": "rukon",
            "sn": "rukon_surname",
            "UserPrincipalName": "[email protected]",
            "memberOf": "['CN=Group A,OU=groups,DC=domain,DC=com', 'CN=Group B,OU=groups,DC=domain,DC=com', 'CN=Group C,OU=groups,DC=domain,DC=com', 'CN=Group D,OU=groups,DC=domain,DC=com', 'CN=Group E,OU=groups,DC=domain,DC=com']"
}

but I would like to format the additional field to output like following

{
            "GivenName": "rukon",
            "sn": "rukon_surname",
            "UserPrincipalName": "[email protected]",
            "memberOf": "['CN=Group A,OU=groups,DC=domain,DC=com',
                          'CN=Group B,OU=groups,DC=domain,DC=com',
                          'CN=Group C,OU=groups,DC=domain,DC=com',
                          'CN=Group D,OU=groups,DC=domain,DC=com',
                          'CN=Group E,OU=groups,DC=domain,DC=com']"
}
Asked By: Frosty.Fluffy

||

Answers:

The problem will be solved if you typecast all the variables to string.
Just use
str(field.sn) str(field.GivenName) and str(field.userPrincipalName) in the dumpData dict.

Updated for second question

d = {"GivenName": "rukon",
"sn": "rukon_surname",
"UserPrincipalName": "[email protected]",
"memberOf": "['CN=Group A,OU=groups,DC=domain,DC=com', 'CN=Group B,OU=groups,DC=domain,DC=com', 'CN=Group C,OU=groups,DC=domain,DC=com', 'CN=Group D,OU=groups,DC=domain,DC=com', 'CN=Group E,OU=groups,DC=domain,DC=com']"}

Used the str.strip to remove the square brackets and split the text using , separator.

d["memberOf"] = d["memberOf"].strip('][').split(', ')

Answered By: Rukon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.