README
ΒΆ
JSON Delete Functionality Examples
This example demonstrates comprehensive JSON deletion functionality provided by the json package.
Features Demonstrated
1. Basic Deletion Operations
- Standard Delete:
json.Delete()- Removes target values but retains null placeholders - Clean Delete:
json.DeleteWithCleanNull()- Removes target values and cleans up null values - Custom Options: Using
ProcessorOptionsfor fine-grained control
2. Array Operations
- Element Deletion: Delete specific array elements by index
- Negative Indexing: Use negative indices to delete from the end
- Range Deletion: Delete ranges of elements using
[start:end]syntax - Open-ended Ranges: Delete from index to end using
[start:]
3. Conditional Deletion
- ForeachReturn Integration: Delete elements based on conditions
- Value-based Filtering: Remove elements matching specific criteria
- Complex Conditions: Support for multiple condition checks
4. Advanced Path Expressions
- Wildcard Deletion: Use
{}to target all elements in arrays/objects - Nested Path Deletion: Delete deeply nested values with dot notation
- Bulk Operations: Delete multiple similar paths in one operation
5. Error Handling
- Invalid Path Handling: Proper error messages for malformed paths
- Non-existent Path Handling: Graceful handling of missing paths (no-op)
- Validation: Input validation with helpful error messages
Usage Examples
Basic Usage
// Standard deletion
result, err := json.Delete(jsonStr, "field.subfield")
// Clean deletion with null cleanup
result, err := json.DeleteWithCleanNull(jsonStr, "array{field}")
// Custom options
opts := &json.ProcessorOptions{
CleanupNulls: true,
CompactArrays: false,
}
result, err := json.Delete(jsonStr, "path", opts)
Array Operations
// Delete by index
result, err := json.Delete(jsonStr, "users[1]")
// Delete by negative index
result, err := json.Delete(jsonStr, "users[-1]")
// Delete range
result, err := json.Delete(jsonStr, "users[1:4]")
// Delete from index to end
result, err := json.Delete(jsonStr, "users[2:]")
Conditional Deletion
result, err := json.ForeachReturn(jsonStr, func(key any, item *json.IterableValue) {
if item.GetBool("active") == false {
err := item.Delete("")
if err != nil {
log.Printf("Failed to delete: %v", err)
}
}
})
Key Benefits
- Flexible: Multiple deletion methods for different use cases
- Safe: Proper error handling and validation
- Efficient: Optimized for performance with large JSON structures
- Intuitive: Easy-to-understand path expressions
- Comprehensive: Covers simple to complex deletion scenarios
ProcessorOptions
The ProcessorOptions struct provides fine-grained control:
CleanupNulls: Remove null values after deletionCompactArrays: Remove null values from arrays and compact themStrictMode: Enable strict validationCacheResults: Cache results for better performance
Path Expression Syntax
field- Delete a simple fieldfield.subfield- Delete nested fieldarray[0]- Delete array element by indexarray[-1]- Delete last array elementarray[1:3]- Delete array rangearray[2:]- Delete from index to endarray{field}- Delete field from all array elementsobject{field}- Delete field from all object values
π Running the Example
To run this example:
cd examples/json_delete
go run example.go
Expected Output
The example will demonstrate:
- β Basic field and array element deletion
- β Custom deletion options (cleanup, compacting)
- β Bulk deletion using path expressions
- β Complex nested deletion scenarios
- β Error handling and edge cases
π Related Examples
Documentation
ΒΆ
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.