Sobes.tech
Junior — Middle

How do you perform JSON string to object conversion in your programming language?

sobes.tech AI

Answer from AI

Most programming languages have built-in functions or libraries for converting a JSON string into an object.

For example, in JavaScript, JSON.parse is used:

const jsonString = '{"name": "Ivan", "age": 30}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // Ivan

In Python, the json module can be used:

import json
json_string = '{"name": "Ivan", "age": 30}'
obj = json.loads(json_string)
print(obj['name'])  # Ivan

In C#, the JsonSerializer class from System.Text.Json or JsonConvert from Newtonsoft.Json is used:

using System.Text.Json;

string jsonString = "{\"name\": \"Ivan\", \"age\": 30}";
var obj = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
Console.WriteLine(obj["name"]); // Ivan

Therefore, you should use the appropriate JSON parsing method for your language.