-
Notifications
You must be signed in to change notification settings - Fork 8
Set
This command allows to set a single value in the data at a given path.
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.
❖ Set Tom's height to 160.
scout set -i People.json -f json "Tom.height=160"The set command can also take several paths with an associated value.
scout set "Tom.height=160" "Robert.hobbies[0]=sleeping"Important
In the next examples, the option -i People.json -f json will be implicitly specified.
It's possible to set the key name in a dictionary rather than the key associated value. Enclose the value with sharp signs: #keyName#.
❖ Set Tom "age" key name to "years".
scout set "Tom.age=#years#"It’s possible to set or add a group value to a path. An array is a list of values separated by commas and enclosed by square brackets. A dictionary is a list of (key, value) parts separated by double points. Those values are separated by commas and enclosed by squared brackets.
❖ Set Tom hobbies to a new array.
scout set "Tom.hobbies=[driving, watching movies]"❖ Set Robert's dictionary to a new value.
scout set "Robert={surname: Bob, score: 20}"It’s possible to nest values in each other. Although it’s not recommenced to nest too much, this can be useful when the value is built programmatically. ❖ Set Robert to a dictionary of arrays.
scout set "Robert={monday: [1, 2], thirsday: [3, 4]}"It's possible to provide an empty array or dictionary when setting a value. If it might be nice to have that for the "set" features, its especially useful with the add command to dynamically fill a dictionary or array.
❖ Set Tom's hobbies to an empty array.
scout set "Tom.hobbies=[]"❖ Set Robert's dictionary to an empty value.
scout set "Robert={}"The library tries to infer the type of a value to set it. For instance "12.3" will be taken as a Double and "true" as a Boolean.
If necessary, it's possible to prevent this automatic type inferring and force one.
- String: enclose the value with slash signs to force the value as a string: /valueAsString/.
- Real/Double: enclose the value with tilde signs to force the value as a real: ~valueToReal~.
❖ Set Suzanne's third movie title to the string value "2.0".
scout set "Suzanne.movies[-1].title=/2.0/"