How do you use the JSON functions to parse and manipulate JSON data?
Posted by CarolTh
Last Updated: July 05, 2024
Using JSON functions to parse and manipulate JSON data largely depends on the programming language or environment you're working in. Below are examples for several popular programming languages:
1. JavaScript
JavaScript has built-in support for JSON through the JSON object. Parsing JSON:
const jsonString = '{"name": "Alice", "age": 25}';
const parsedData = JSON.parse(jsonString);

console.log(parsedData.name); // Output: Alice
console.log(parsedData.age);  // Output: 25
Manipulating JSON:
parsedData.age += 1; // Increment age
const updatedJsonString = JSON.stringify(parsedData);

console.log(updatedJsonString); // Output: {"name":"Alice","age":26}
2. Python
Python has a built-in json module. Parsing JSON:
import json

json_string = '{"name": "Alice", "age": 25}'
parsed_data = json.loads(json_string)

print(parsed_data['name'])  # Output: Alice
print(parsed_data['age'])   # Output: 25
Manipulating JSON:
parsed_data['age'] += 1  # Increment age
updated_json_string = json.dumps(parsed_data)

print(updated_json_string)  # Output: {"name": "Alice", "age": 26}
3. Java
In Java, you can use libraries like org.json, Gson, or Jackson. Parsing JSON with org.json:
import org.json.JSONObject;

String jsonString = "{\"name\": \"Alice\", \"age\": 25}";
JSONObject jsonObject = new JSONObject(jsonString);

System.out.println(jsonObject.getString("name")); // Output: Alice
System.out.println(jsonObject.getInt("age"));     // Output: 25
Manipulating JSON:
jsonObject.put("age", jsonObject.getInt("age") + 1);
String updatedJsonString = jsonObject.toString();

System.out.println(updatedJsonString); // Output: {"name":"Alice","age":26}
4. C#
In C#, you can use Newtonsoft.Json (Json.NET) or System.Text.Json. Parsing JSON using Newtonsoft.Json:
using Newtonsoft.Json;
using System;

string jsonString = "{\"name\": \"Alice\", \"age\": 25}";
dynamic parsedData = JsonConvert.DeserializeObject(jsonString);

Console.WriteLine(parsedData.name); // Output: Alice
Console.WriteLine(parsedData.age);  // Output: 25
Manipulating JSON:
parsedData.age += 1; // Increment age
string updatedJsonString = JsonConvert.SerializeObject(parsedData);

Console.WriteLine(updatedJsonString); // Output: {"name":"Alice","age":26}
5. SQL
In SQL databases like PostgreSQL or MySQL, you can use JSON functions to manipulate JSON data at the database level. PostgreSQL example:
-- Create table with a JSON column
CREATE TABLE users (data JSON);

-- Inserting JSON data
INSERT INTO users (data) VALUES ('{"name": "Alice", "age": 25}');

-- Query and manipulate JSON data
SELECT
    data ->> 'name' AS name,
    (data ->> 'age')::int + 1 AS incremented_age
FROM users;
Summary
In each programming language, the common tasks involve parsing a JSON string into an object, manipulating that object (e.g., modifying values, adding new fields), and then serializing it back to a JSON string. Make sure to handle any exceptions or errors that may arise during parsing or serialization for robust applications.