WOODY'S
FINDINGS

Data 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.
Also, a CSV export is offered (whereas the opposite operation will be offered in Scout 3.1.0).

About the conversion from XML

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.

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).

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

Supported formats

Simple export

Export a JSON file to a Plist one.

scout read -i People.json -e plist >> People.plist


Set a value and export the data

Set a value in the data and export it to another format

scout set -i People.yml -f yaml "Tom.height=170" -e xml

XML root element

When exporting to XML, the name of the input file will be used as the root elment name. If no input file is used, the root element will be named "root".


CSV export

It's possible to output an array or a dictionary of arrays as CSV, with the default separator ';' or a custom one.

Array

The command

scout read -i People.json "Suzanne.movies" --csv

shoult output
awards;title
Best speech for a silent movie;Tomorrow is so far
Best title;Yesterday will never go
NULL;What about today?

Use the --csv-sep option to choose the separator for the Command-line (default parameter in Swift).

Dictionary of arrays

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.
The command

scout read -i People.json "#Tom|Robert#.hobbies" --csv
will output:
Tom_hobbies;cooking;guitar
Robert_hobbies;video games;party;tennis

Note that the CSV can be exported with the --output | -o command


CSV import

Starting with Scout 4.0.0, it's possible to import a CSV to a supported format.
A cool feature 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 movies
will 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.
Here is an example for the command.
scout csv -i people.csv -s ";" -f json --headers

The headers|--no-headers flag is needed to specify whether the CSV string begins with headers. It’s also possible to use the standard input to provide the CSV data.