-
Notifications
You must be signed in to change notification settings - Fork 8
Delete
This command outputs delete a single value (e.g. a string) or a group value (e.g. a dictionary) for a given path. It can act recursively, deleting the group values that are left empty.
The examples on this page will refer to the JSON file "People.json" that can be found in the playground.
Tip
Any of the four People files could be used.
People.json
{
"Tom" : {
"age" : 68,
"hobbies" : [
"cooking",
"guitar"
],
"height" : 175
},
"Robert" : {
"age" : 23,
"hobbies" : [
"video games",
"party",
"tennis"
],
"running_records" : [
[
10,
12, 9,
10
],
[ 9,
12,
11
]
],
"height" : 181
},
"Suzanne" : {
"job" : "actress",
"movies" : [
{
"title" : "Tomorrow is so far",
"awards" : "Best speech for a silent movie"
},
{
"title" : "Yesterday will never go",
"awards" : "Best title"
},
{
"title" : "What about today?"
}
]
}
}The option -i|--input option can be used to specify a file as input. Otherwise Scout can read the input stream.
The option -o|--output option can be used to specify a file where the modified data should be written. Otherwise Scout will output it in the terminal.
The option -m|--modify option can be used to specify a file as input and as output.
❖ Delete Tom's height.
scout delete -i People.json -f json "Tom.height"Important
In the next examples, the option -i People.json -f json will be implicitly specified.
The command can delete a group value if it is left empty. For instance when an array has no more elements.
The -r|--recursive option has to be specified.
❖ Delete Tom's 2 hobbies and recursively deletes the Tom's hobbies array (the higher elements have to be deleted first).
scout delete -r "Tom.hobbies[1]" "Tom.hobbies[0]"Caution
When deleting several paths at once, be careful to the order of the paths when dealing with indexes in a same array. Start with the last index to be deleted and choose a descending order.
It's possible to target a slice of an array from a lower bound to an upper bound. The upper bound is included. When a array is sliced, it's possible to continue the path with a next element that elements in the slice have in common.
A slice is specified with square brackets and a double point ':' between the bounds: '[lower:upper]'. No lower means 0 like [:10] and no upper means the last index like [10:].
Use a negative index to target the last nth elements like [-4:] to target the last 4 elements or [-4:-2] to target from the last fourth to the last second element.
❖ Delete Robert's first two hobbies.
scout delete "Robert.hobbies[:1]"❖ Delete Suzanne's movies titles.
scout delete "Suzanne.movies[:].title"It's possible to target specific keys in a dictionary with a regular expression.
When a dictionary is filtered, it's possible to continue the path with a next element that elements in the slice have in common. If the provided regular expression is invalid, an error will be returned.
This element is enclosed by sharp '#' signs. For instance #.*device.*# to target all the keys in a dictionary containing the word device.
❖ Delete Tom keys that start with 'h'.
scout delete "Tom.#h.*#"❖ Get Tom and Robert first hobby.
scout delete "#Tom|Robert#.hobbies[0]"It's possible to mix the array slicing and dictionary filtering features.
❖ Delete Tom and Robert first two hobbies.
scout delete "#Tom|Robert#.hobbies[:1]"