Junior — Middle
Which library is used for converting data from JSON format to objects and vice versa?
sobes.tech AI
Answer from AI
The standard library json is often used in Python for converting data from JSON format to objects and back.
Example of usage:
import json
# Converting a JSON string to a Python object
json_str = '{"name": "Ivan", "age": 30}'
data = json.loads(json_str)
print(data['name']) # Outputs: Ivan
# Converting a Python object to a JSON string
python_obj = {'city': 'Moscow', 'population': 12000000}
json_data = json.dumps(python_obj, ensure_ascii=False)
print(json_data) # Outputs: {"city": "Moscow", "population": 12000000}
For other programming languages, there are their own libraries, for example, in JavaScript, it is the built-in JSON object, in Java — libraries like Jackson or Gson.