Convert string output to JSON

I get some data from an external system (Salesforce Marketing Cloud) via the API, and I return the data in the following format:

Results: [(List){ Client = (ClientID){ ID = 113903 } PartnerKey = None CreatedDate = 2013-07-29 04:43:32.000073 ModifiedDate = 2013-07-29 04:43:32.000073 ID = 1966872 ObjectID = None CustomerKey = "343431CD-031D-43C7-981F-51B778A5A47F" ListName = "PythonSDKList" Category = 578615 Type = "Private" Description = "This list was created with the PythonSDK" ListClassification = "ExactTargetList" }] 

This is painfully close to JSON, I would like to format it as a JSON file so that I can import it into our database. Any ideas on how I can do this?

thanks

+5
source share
2 answers

It looks like an object called suds, which is already in Python. Fuel-SDK uses it.

The foam object has already done this for you. Just call the attribute you are looking for.


However, if you want it to be like a dict, then a common function is used for this:

 from suds.sudsobject import asdict def recursive_asdict(d): out = {} for k, v in asdict(d).iteritems(): if hasattr(v, '__keylist__'): out[k] = recursive_asdict(v) elif isinstance(v, list): out[k] = [] for item in v: if hasattr(item, '__keylist__'): out[k].append(recursive_asdict(item)) else: out[k].append(item) else: out[k] = v return out def suds_to_json(data): return json.dumps(recursive_asdict(data)) 

The first will convert it to dict, and the second to the correct json.

Some useful links: https://fedorahosted.org/suds/wiki/Documentation

+1
source

To convert an array, you can use var jsonString = JSON.stringify (yourArray);

+2
source

All Articles