function::json_add_array - Linux
Overview
The function::json_add_array
command adds a new array to a JSON object or array. This is useful for creating or modifying JSON data structures in a programmatic way.
Syntax
function::json_add_array KEY JSON_OBJECT
Options/Flags
| Option | Description | Default Value |
| ——- | ——————————————————————————————————————————————————————————————————- | ————– |
| KEY
| The key of the array to be added. If the key already exists, the value will be replaced. | N/A |
| JSON_OBJECT
| The JSON object or array to which the new array will be added. | N/A |
Examples
Example 1: Adding an array to a JSON object
$ echo '{"name": "John Smith"}' | function::json_add_array "addresses" '[{"street": "123 Main Street", "city": "Anytown", "state": "CA", "zip": "12345"}]'
{"name": "John Smith", "addresses": [{"street": "123 Main Street", "city": "Anytown", "state": "CA", "zip": "12345"}]}
Example 2: Adding an array to a JSON array
$ echo '[{"name": "John Smith"}, {"name": "Jane Doe"}]' | function::json_add_array "2" '[{"age": 30}]'
[{"name": "John Smith"}, {"name": "Jane Doe"}, {"age": 30}]
Common Issues
- Error: Key already exists: The key you specified already exists in the JSON object or array. To replace the existing value, use the
function::json_set
command instead. - Error: Invalid JSON: The JSON provided is not valid. Check for syntax errors or inconsistencies in the data structure.
Integration
The function::json_add_array
command can be combined with other Linux tools to create complex JSON data structures. For example, you could use jq
to parse JSON output and then use function::json_add_array
to modify the data before writing it back to a file.
Related Commands
function::json_add_object
function::json_get_object
function::json_set