Forum Discussion

jfpelletier's avatar
jfpelletier
Contributor
10 months ago
Solved

How to get all values of a key from objects in a list?

Hello all, I'm a bit blocked at the moment, hopefully someone can help me again... 🙂 This time I have simple objects stored in an array that looks like this: [ { "language": "German", "id":...
  • tstack's avatar
    8 years ago

    The CSV Parser supports a seconds input view that you can use to feed it a simple schema. So, you should be able to read in the XML file with the schema and then convert it into the format that the CSV Parser expects. The following is a pipeline that does just that, you should be able to replace the Constant snaps with File Readers to make it work for you.

    CSVSchema_2018_03_12.slp (8.1 KB)

    Here’s the expression in the Mapper that is doing the conversion:

    {}.extend($SchemaTables.SchemaTable.sort((x, y) => x.ColumnOrdinal - y.ColumnOrdinal).map(x => [x.ColumnName, "string"]))

    It first sorts the SchemaTable array by the ColumnOrdinal and then uses a map() to produce an array of key/value pairs. Those pairs are then fed into the extend() method to create the output document that is turned into the CSV schema file.

    The problem could be attacked in other ways as well. For example, the XML schema file could be converted into an expression library and imported into the pipeline. Then, a mapper could be added that used the mapKeys() method to lookup the column number in the schema library and mapped it to the column name.