Python JSON object serialization
What's inside this article
⌄
- Convert json to object python
- Convert object to json python
- Build object from json python
Build Object from JSON
import json
import YourObject from your_file
json_string = "<paste_json_string_here>"
json_object = json.loads(json_string)
u = YourObject(**json_object)
Convert Object to JSON
import json
import YourObject from your_file
your_object = YourObject(
propertyName1="value1",
propertyName2="value2",
additionalProperties={
"additionalProperty1": "123",
"additionalProperty1": "example_2"
},
)
json_string = json.dumps(your_object, default=lambda o: o.__dict__)
print(json_string)
If you need to rename some properties in the target json, you can make it this way:
json_string.replace("additional_property1", "additionalProperty1")