-
Notifications
You must be signed in to change notification settings - Fork 8
Conversion
The library offers data formats conversion between the supported format with any of the read, set, delete and add commands. For instance, it's possible to set a value in JSON data and export it to a Plist format.
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?"
}
]
}
}Conversion from and to JSON, Plist, YAML and XML.
Important
The conversion from XML can change the data structure when a tag has one ore more attributes. In such a case, the key will be transformed to a dictionary with two keys: "attributes" and "value". The "attribute" key will be a dictionary holding the attributes and the "value" key will hold the value of the key.
❖ Export a JSON file to a Plist one.
scout read -i People.json -e plist >> People.plist❖ Set a value in the data and export it to another format.
scout set -i People.yml -f yaml "Tom.height=170" -e xmlNote
When exporting to XML, the name of the input file will be used as the root element name. If no input file is used, the root element will be named "root".
It's possible to output an array or a dictionary of arrays as CSV, with the default separator ';' or a custom one.
❖ Export Suzanne's movies array to a CSV file.
scout read -i People.json "Suzanne.movies" --csvOutput
awards;title
Best speech for a silent movie;Tomorrow is so far
Best title;Yesterday will never go
NULL;What about today?Note
Use the --csv-sep option to choose the separator.
It's also possible to export a dictionary of arrays as mentioned above. In this case, the first element of each line is the name of the key associated to the array.
❖ Export Tom and Robert hobbies
scout read -i People.json "#Tom|Robert#.hobbies" --csvOutput
Tom_hobbies;cooking;guitar
Robert_hobbies;video games;party;tennisNote
The CSV data can be exported with the --output | -o command
It's also possible to import a CSV to a supported format.
When working with named headers is that they will be treated as paths. This can shape very precisely the structure of the converted data. For instance, the following CSV
name.first;name.last;hobbies[0];hobbies[1]
Robert;Roni;acting;driving
Suzanne;Calvin;singing;play
Tom;Cattle;surfing;watching movieswill be converted to the following JSON structure.
[
{
"hobbies" : [
"acting",
"driving"
],
"name" : {
"first" : "Robert",
"last" : "Roni"
}
},
{
"hobbies" : [
"singing",
"play"
],
"name" : {
"first" : "Suzanne",
"last" : "Calvin"
}
},
{
"name" : {
"first" : "Tom",
"last" : "Cattle"
},
"hobbies" : [
"surfing",
"watching movies"
]
}
]When there are no headers, the input will be treated as a one or two dimension(s) array.
❖ Import a CSV file "people.csv" with headers in a JSON format.
scout csv -i people.csv -s ";" -f json --headersThe --headers| --no-headers flag is needed to specify whether the CSV string begins with headers.
Note
It’s also possible to use the standard input to provide the CSV data.