Understanding JSONC: JSON with Comments
JSONC (JSON with Comments)
JSONC (JavaScript Object Notation with Comments) is an extension of JSON that allows adding comments. Regular JSON does not permit comments, which can make it difficult to explain or annotate complex data structures within a JSON file. JSONC solves this by allowing single-line (//) and multi-line (/* */) comments, making configuration files and data more readable and maintainable.
Syntax of JSONC
JSONC follows the basic syntax of JSON with the addition of comments. Comments can be added using:
1. Single-line comments: // Comment here 2. Multi-line comments: /* This is a multi-line comment */
Example JSONC Structure:
{ // This is a single-line comment "name": "Sara", /* Inline comment for the name key */ "age": 30, "skills": [ "Python", "JavaScript" ] /* A list of skills */ }
Examples of JSONC
Example 1: Annotating Configuration Files
Code:
{
// Application settings
"app": {
"name": "MyApp",
"version": "1.0.0"
},
/* Database configuration */
"database": {
"host": "localhost",
"port": 3306,
"user": "admin",
"password": "secret" // Change this to your actual password
}
}
Example 2: Documenting API Responses
Code:
{
// Metadata about the response
"meta": {
"timestamp": "2025-01-03T10:00:00Z", // Time of the response
"status": "success"
},
/* User details section */
"data": {
"id": 12345,
"name": " Ceyhun Maria",
"email": "[email protected]"
}
}
Example 3: Reading and Processing JSONC in Python
To work with JSONC in Python, you can strip comments using the jsoncomment library or preprocess the file manually. Below is an example of processing JSONC:
Code:
# Import necessary libraries
import json
import re # Regular expressions for comment removal
# Define a JSONC string
jsonc_data = '''
{
// User information
"name": "Sara",
"age": 25, /* Inline comment */
"skills": ["Python", "Data Analysis"]
}
'''
# Function to remove comments from JSONC
def remove_comments(jsonc_str):
# Remove single-line comments (// ...)
jsonc_str = re.sub(r'//.*?(\n|$)', '', jsonc_str)
# Remove multi-line comments (/* ... */)
jsonc_str = re.sub(r'/\*.*?\*/', '', jsonc_str, flags=re.DOTALL)
return jsonc_str
# Remove comments from the JSONC string
cleaned_json = remove_comments(jsonc_data)
# Parse the cleaned JSON string
data = json.loads(cleaned_json) # Convert cleaned JSON to a Python dictionary
# Print the parsed data
print(data) # Output: {'name': 'Sara', 'age': 25, 'skills': ['Python', 'Data Analysis']}
Output:
{'name': 'Sara', 'age': 25, 'skills': ['Python', 'Data Analysis']}
Explanation:
- Removes single-line comments starting with // and ending at the next newline.
- Removes multi-line comments enclosed between /* and */.
- flags=re.DOTALL ensures that .*? matches across multiple lines.
- Converts the cleaned JSON string (without comments) into a Python dictionary using Python’s built-in json module.
1. re.sub(r'//.*?(\n|$)', '', jsonc_str):
2. re.sub(r'/\*.*?\*/', '', jsonc_str, flags=re.DOTALL):
3. json.loads(cleaned_json):
Note:
- Improves Readability: Comments help document configurations or complex data.
- Eases Collaboration: Teams can understand the purpose of each key-value pair.
- Ideal for Configuration Files: Widely used in tools like Visual Studio Code for settings.json files.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics