How to use 'json stringify online' in Python

Every line of 'json stringify online' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
56def stringify(obj):
57 """Convert object to string.
58 If the object is a dictionary-like object or list,
59 the objects in the dictionary or list will be converted to strings, recursively.
60
61 Returns: If the input is dictionary or list, the return value will also be a list or dictionary.
62
63 """
64 if isinstance(obj, collections.Mapping):
65 obj = copy.deepcopy(obj)
66 obj_dict = {}
67 for key, value in obj.items():
68 obj_dict[key] = stringify(value)
69 return obj_dict
70 elif isinstance(obj, list):
71 str_list = []
72 for item in obj:
73 str_list.append(stringify(item))
74 return str_list
75 else:
76 try:
77 json.dumps(obj)
78 return obj
79 except TypeError:
80 return str(obj)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

42def stringify(data):
43 str_data = ""
44 if type(data) == dict or type(data) == list:
45 return json.dumps(data)
46 else:
47 try:
48 str_data = str(data)
49 return str_data
50 except Exception as e:
51 return data

Related snippets