WOODY'S
FINDINGS

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 recursiverly, deleting the group values that are left empty.

The examples on this page will refer to this JSON file that can be found in the playground (you might want to copy/paste or to download it somewhere to keep it in sight).
Note that any of the four People files could be used.

{
  "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?"
      }
    ]
  }
}

Input and output

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.


Simple example

Delete Tom's height.

scout delete -i People.json -f json "Tom.height"
In the next examples, the option -i People.json -f json will be implicitly specified.

The delete command can also take several paths with an associated value.

scout delete "Tom.height" "Robert.hobbies[0]"


Common

Invalid paths

If the path is invalid, the command will return/throw an error with a proper message. A path can be invalid for several reasons. For instance:

This command will return an error because the key "score" does not exist in Tom's dictionary.

scout delete "Tom.score"
When that's possible, the program will display a message to offer another possible key name. For instance the command
scout delete "Tom.heiht"
will return an error but will also inform that the key 'height' is near the 'heiht' word.


Array subscripting

It's possible to access an element in an array by specifying its index.
When the index is negative, this targets an element starting from the end of the array.
This figure gives an example with the 'ducks' array.

["Riri", "Fifi", "Loulou", "Donald", "Daisy"]
[  0   ,   1   ,    2    ,    3    ,    4   ] (Positive)
[ -5   ,  -4   ,   -3    ,   -2    ,   -1   ] (Negative)

Delete Robert's second hobby.

scout delete "Robert.hobbies[1]"
Targets Robert's last hobby.
scout delete "Robert.hobbies[-1]"


Delete command specificities

Delete if empty

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]"

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.


Array slicing

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"


Keys filtering

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 thrown/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]"


Mixing

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]"