Listing paths
This features allows to list all the paths in the data. It can be useful to iterate over the values in a shell script and then play with the read
, set
, delete
and add
commands.
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?"
}
]
}
}
Simple example
List all the paths in the file People.plist.
Use the option -i|--input
to specify a file as input. Otherwise Scout can read the input stream.
scout paths -i People.json -f json
Output
The function PathExplorer.listPath()
will return an array of Path
s in Swift. With the command-line tool, those paths are printed in the terminal.
Robert
Robert.age
Robert.height
Robert.hobbies
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Robert.running_records
Robert.running_records[0]
Robert.running_records[0][0]
Robert.running_records[0][1]
Robert.running_records[0][2]
Robert.running_records[0][3]
Robert.running_records[1]
Robert.running_records[1][0]
Robert.running_records[1][1]
Robert.running_records[1][2]
Suzanne
Suzanne.job
Suzanne.movies
Suzanne.movies[0]
Suzanne.movies[0].awards
Suzanne.movies[0].title
Suzanne.movies[1]
Suzanne.movies[1].awards
Suzanne.movies[1].title
Suzanne.movies[2]
Suzanne.movies[2].title
Tom
Tom.age
Tom.height
Tom.hobbies
Tom.hobbies[0]
Tom.hobbies[1]
Target single or group values
When listing paths, it's possible to target only single values (e.g. string, number...), group values (e.g. array, dictionary) or both. The default target is both single and group.
List all the paths leading to single values in the file People.xml.
scout paths -i People.xml -f xml --single
Output
Robert.age
Robert.height
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Robert.running_records[0][0]
Robert.running_records[0][1]
Robert.running_records[0][2]
Robert.running_records[0][3]
Robert.running_records[1][0]
Robert.running_records[1][1]
Robert.running_records[1][2]
Suzanne.job
Suzanne.movies[0].awards
Suzanne.movies[0].title
Suzanne.movies[1].awards
Suzanne.movies[1].title
Suzanne.movies[2].title
Tom.age
Tom.height
Tom.hobbies[0]
Tom.hobbies[1]
List all the paths leading to group values in the file People.xml.
scout paths -i People.xml -f xml --group
Output
Robert
Robert.hobbies
Robert.running_records
Robert.running_records[0]
Robert.running_records[1]
Suzanne
Suzanne.movies
Suzanne.movies[0]
Suzanne.movies[1]
Suzanne.movies[2]
Tom
Tom.hobbies
Initial path
Optionally provide a path from which the paths should be listed. The special elements like array slices or dictionary keys filters are supported
List all the paths in the file People.yml in Robert dictionary.
scout paths "Robert" -i People.yml -f yaml
Output
Robert.age
Robert.height
Robert.hobbies
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Robert.running_records
Robert.running_records[0]
Robert.running_records[0][0]
Robert.running_records[0][1]
Robert.running_records[0][2]
Robert.running_records[0][3]
Robert.running_records[1]
Robert.running_records[1][0]
Robert.running_records[1][1]
Robert.running_records[1][2]
List all the paths in the file People.yml in Robert and Tom dictionary.
scout paths -i People.yml -f yaml "#Robert|Tom#"
Output
Robert
Robert.age
Robert.height
Robert.hobbies
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Robert.running_records
Robert.running_records[0]
Robert.running_records[0][0]
Robert.running_records[0][1]
Robert.running_records[0][2]
Robert.running_records[0][3]
Robert.running_records[1]
Robert.running_records[1][0]
Robert.running_records[1][1]
Robert.running_records[1][2]
Tom
Tom.age
Tom.height
Tom.hobbies
Tom.hobbies[0]
Tom.hobbies[1]
List all the paths leading to Suzanne's movies titles in the file People.yml.
scout paths -i People.yml -f yaml "Suzanne.movies[:].title"
Output
Suzanne.movies[0].title
Suzanne.movies[1].title
Suzanne.movies[2].title
Filter the keys
It's possible to provide a regular expression to filter the paths final key. Only the paths whose final value is validated by the regular expression will be retrieved.
List all the paths leading to a key "hobbies" in the file People.json.
scout paths -i People.json -f json -k "hobbies"
Output
Robert.hobbies
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Tom.hobbies
Tom.hobbies[0]
Tom.hobbies[1]
List all the paths leading to a key starting with "h" in the file People.json.
scout paths -i People.json -f json -k "h.*"
Output
Robert.height
Robert.hobbies
Robert.hobbies[0]
Robert.hobbies[1]
Robert.hobbies[2]
Tom.height
Tom.hobbies
Tom.hobbies[0]
Tom.hobbies[1]
Filter the values
The values can be filtered with one ore more predicates. When such a filter is speicified, only the single values are targeted.
A path whose value is validated by one of the provided predicates is retrieved.
A predicate will contain the variable 'value' that will be replaced to evaluate each value.
List the paths whose value is below 70.
scout paths -i People.plist -f plist -v "value < 70"
Output
Robert.age
Robert.running_records[0][0]
Robert.running_records[0][1]
Robert.running_records[0][2]
Robert.running_records[0][3]
Robert.running_records[1][0]
Robert.running_records[1][1]
Robert.running_records[1][2]
Tom.age
List the paths whose value is greater than or equal to 20 and lesser than 70.
scout paths -i People.plist -f plist -v "value >= 20 && value < 70"
Output
Robert.age
Tom.age
List the paths whose value starts with "guit" (case sensitive).
scout paths -i People.json -f json -v "value hasPrefix 'guit'"
Output
Tom.hobbies[1]
List the paths whose value starts with "guit" or are greater than 20 (case sensitive).
scout paths -i People.json -f json -v "value hasPrefix 'guit'" -v "value > 20"
Output
Robert.age
Robert.height
Tom.age
Tom.height
Tom.hobbies[1]
To learn more about the possibilities offered by the predicates, it's possible to run scout doc -a predicates
or to read this dedicated page.
Mixing up
All the features to filter the path can be mixed up.
List paths leading to Robert hobbies that contain the word "game".
scout paths -i People.yml -f yaml "Robert.hobbies" -v "value contains 'games'"
Output
Robert.hobbies[0]
List paths leading to Robert or Tom hobbies array (group values).
scout paths -i People.yml -f yaml "#Tom|Robert#" -k "ho.*" --group
Output
Robert.hobbies
Tom.hobbies
List paths leading to Suzanne's movies titles that contains the word "today".
scout paths -i People.json -f json "Suzanne.movies[:].title" -v "value contains 'today'"
Output
Suzanne.movies[2].title