Question

How can I easily expose attributes from XMLFlattener or JSONFlattener

  • 20 June 2017
  • 3 replies
  • 36 views

Userlevel 4
Badge +13

Both the XMLFlattener and JSONFlattener (and the Flatten options in the XMLFragmenter and JSONFragmenter) give you the option to expose the newly created attribute names to the workspace.

However, if there are a lot attributes, it can be a pain to type them all in. As well, any changes to the input XML or JSON may result in new attributes to expose.


3 replies

Userlevel 2
Badge +17

The attached workspace, importattributenames.fmw, contains a process that allows you to expose the attributes with a minimum of effort.

Copy the transformers from Part 1 into your workspace, then set the output file name in the AttributeFileWriter. When you run the workspace, the attributes from all features produced by the JSONFlattener will be merged into single representative feature. The attribute names from this feature will be exploded into a list, which is then concatenated into a comma separated line. The CSV line is then written to the temp file you set in the AttributeFileWriter.

After Part 1 is run, you can add the AttributeExposer from Part 2 just after the JSONFlattener. The AttributeFlattener's interface includes an Import button. Import the temp file using the CSV reader, which will read the single line as the field names. In the CSV reader Parameters, you can pick the attribute names you want to expose to the rest of the workspace.Once you have set up the AttributeExposer, you can disable the Part 1 transformers. If the JSON input changes, you can re-run this process to quickly update the exposed attributes from the new input.

Badge +6

Another approach to exposing JSON attributes after JSONFragmenter or JSONFlattener is as follows: Add an AttributeExposer after the Fragmenter and use the 'Import' button at the bottom. Select the JSON or XML reader and use the same file / feature path you used in the Fragmenter. You can then use the appropriate Reader to read the schema and import all the field names on to your schema here so you don't need to type them. This works with URLs as well as files since the XML and JSON readers accept URLs. With this approach you don't need an intermediate CSV file as in Part 1 above. However, you do need to know how to configure the appropriate JSON Query or XML feature path, which is a lot easier now that we have the tree control in the JSON and XML readers.

Badge

Another approach to exposing JSON attributes after JSONFragmenter or JSONFlattener is as follows: Add an AttributeExposer after the Fragmenter and use the 'Import' button at the bottom. Select the JSON or XML reader and use the same file / feature path you used in the Fragmenter. You can then use the appropriate Reader to read the schema and import all the field names on to your schema here so you don't need to type them. This works with URLs as well as files since the XML and JSON readers accept URLs. With this approach you don't need an intermediate CSV file as in Part 1 above. However, you do need to know how to configure the appropriate JSON Query or XML feature path, which is a lot easier now that we have the tree control in the JSON and XML readers.

I like this approach! If it can be done in 1 go, that's always an advantage, I think.

 

However, I think that would make a good idea: why not have an Import button in the JSON/XMLFlattener/Fragmenter so you can do this all in the same transformer? Same goes for a few other transformers (e.g. PythonCaller) that allow for attribute exposure.

 

 

Reply