JSONUpdater
Modifies JSON documents by performing one or more actions:
- Inserting a new value into a JSON object or array
- Replacing an existing value in a JSON object or array
- Merging the contents of two JSON objects or arrays
- Removing a value from a JSON object or array
- Appending a value to a JSON array
- Renaming a key in a JSON object.
For each document update, a JSON path expression is used to identify the location of the JSON object or array which is to be modified. A second parameter names the specific object key or array index which is to be changed. Note that the path may refer to more than one item. If this is the case, all the items will be modified.
For modifications that replace existing items or insert new items, two parameters are given which specify the value to be added to the document. This value may be given as a simple text value, or it may be an XQuery expression, a JSON object or a JSON array.
The Update features provide the attribute values used in the document update operations. The Document feature is only used to supply the JSON document which is to be updated.
For each feature that enters the Update port, all of the update operations listed in the transformer will be applied to each feature that entered the transformer through the Document port. Therefore, no changes will be made to the JSON document if no features enter the transformer through the Update port.
Note that if the document is entered directly into the transformer parameters, it may be processed in two different ways:
- If no Document features are given to the transformer, a new feature will be created to store the document. Each of the updates will be applied to the document, and it will then be output through the Document port.
- If Document features are given to the transformer, each of the updates will be applied to the document, and these features will be output from the transformer.
Input Ports
Input features contain the JSON documents to be modified. The documents can be contained in an attribute, entered directly into the transformer, or contained in a file referenced by an attribute value.
For each feature which enters this input port, each of the updates listed in the transformer will be executed on each document which entered via the Document input port. These features will contain the attributes values which are used in the document updates.
Output Ports
The Document input features will be output through this port. These features will contain the updated document, which will be stored in the attribute named in the Result Attribute parameter. The updated document will always be saved as a utf-8 encoded attribute value, regardless of the encoding of the original document.
Parameters
Transformer
This parameter is used to specify which update features will modify a document. When this parameter is set to a list of attributes, Document features will only be modified by Update features that share the same values for these attributes. The document will be modified by all of the update features with the same attribute values. Note that a document will not be updated if no Update features with the same values for the “group by” attributes are provided to the transformer.
Process At End (Blocking): This is the default behavior. Processing will only occur in this transformer once all input is present.
Process When Group Changes (Advanced): This transformer will process input groups in order. Changes of the value of the Group By parameter on the input stream will trigger processing on the currently accumulating group. This may improve overall speed (particularly with multiple, equally-sized groups), but could cause undesired behavior if input groups are not truly ordered.
There are two typical reasons for using Process When Group Changes (Advanced) . The first is incoming data that is intended to be processed in groups (and is already so ordered). In this case, the structure dictates Group By usage - not performance considerations.
The second possible reason is potential performance gains.
Performance gains are most likely when the data is already sorted (or read using a SQL ORDER BY statement) since less work is required of FME. If the data needs ordering, it can be sorted in the workspace (though the added processing overhead may negate any gains).
Sorting becomes more difficult according to the number of data streams. Multiple streams of data could be almost impossible to sort into the correct order, since all features matching a Group By value need to arrive before any features (of any feature type or dataset) belonging to the next group. In this case, using Group By with Process At End (Blocking) may be the equivalent and simpler approach.
Note: Multiple feature types and features from multiple datasets will not generally naturally occur in the correct order.
As with many scenarios, testing different approaches in your workspace with your data is the only definitive way to identify performance gains.
JSON Document
This parameter is used to specify the type of JSON document which is to be modified. The JSON may come from the input features (Parameter Value: Text or Attribute), or it may come from an external file. (Parameter Value: JSON File)
When the JSON Input parameter is set to ‘Text or Attribute’, this parameter is used to specify the JSON text. The parameter may be set to an attribute value, or the JSON document may be entered into the parameter directly.
When the JSON Input parameter is set to ‘JSON File’, this parameter is used to specify the location of the file. The file path may be entered directly into the parameter, or the path may come from an attribute value.
Updates
Each row in the Updates table specifies a single update operation.
This parameter specifies the type of modification to apply to the XML document. The possible updates are:
Update Type | Description |
---|---|
Insert Into Object |
Inserts a new key / value pair into an object. The name of the key is taken from the ‘Object/Array Index’ column. The inserted value is taken from the Value parameter. |
Merge Objects |
Copies all the key / value pairs from one object into another. If the Value parameter is not a JSON object, or an XQuery expression which evaluates to a object, the update will fail. |
Insert Into Array |
Inserts a new value into an array, at a specified position. The array position (which starts counting at 0) is taken from the ‘Object/Array Index’ column, and should be an integer. The new value is taken from the Value column. |
Append to Array |
Appends a new value to the end of an array. |
Merge Arrays |
Copies all values from one array into another. If the Value parameter is not a JSON array, or an XQuery expression which evaluates to an array, the update will fail. |
Replace Object Value |
Replaces the value of an object key. The key whose value is to be replaced is taken from the ‘Object/Array Index’ parameter. |
Replace Array Value |
Replaces the value at a specified position within an array. The array index of the value to replace is taken from the ‘Object/Array Index’ column. The index should be an integer. Array indices being counting at 0. |
Rename Object Key |
Changes an object key name. The key to rename is taken from the ‘Object/Array Index’ parameter. |
Delete from Object |
Removes a key / value pair from an object. The key which is to be removed is taken from the ‘Object/Array Index’ parameter. |
Delete from Array |
Removes a value from an array. The array index of the value to remove is taken from the ‘Object/Array Index’ column. The index should be an integer. Array indices being counting at 0. |
This parameter is used to identify the JSON object or array which is to be updated. For updates which modify a specific object key or array index, the key / index is identified using the ‘Object/Array Index’ parameter.
JSON paths use a simple text syntax to trace a route through a JSON document, from the top level object or array, down to the object or array which is being modified.
All JSON documents must have a single object or array as the root value. In a JSON path, this root value is identified by the ‘json’ keyword. Thus, every JSON path begins with this keyword. Square bracket syntax is used to identify each level of the path.
An integer can be used to identify a position within an array:
- json[0] – If the root value is an array, this path refers to the first value in the array. If the root value is an object, this path will not resolve to any value.
- json[4] – If the root value is an array, this path refers to the fifth value in the array.
A quoted string can be used to refer to the value of an object key:
- json["location"] – If the root value is an object, this path refers to the value of the ‘location’ key in the object. If the root value is an array, this path will not resolve to any value.
- json["features"] – Refers to the value of the ‘features’ key in the root object.
These indices may be repeated, to have the path go deeper into the JSON document. It is important to know the structure of the document, so that the types of indices (object or array) are correct.
- json[0]["location"] – This path refers to the ‘location’ object key in the first element in the root array.
- json["features"][0][ "location"] – This path refers to the value of the ‘location key in the first item in the ‘features’ array of the root object. For example, this could apply to the following (incomplete) document:
{
"features" : [
{ "location" : { ... } }
]
}
A wildcard may be used to refer to all elements of an array, or all values in an object. A * character is used to denote a wildcard.
- json[*] – If the root value is an array, this path refers to all values in the array. If the root value is an object, this path refers to the values of all keys in the object.
- json[*]["location"] – This path refers to the ‘location’ key of all child values of the root object or array.
- json["features"][*] – This path refers to all the values contained in the value of the ‘features’ key in the root object.
In order to use an attribute value in a JSON path, the @Value FME function should be used. If the attribute value is an object key, the @Value function should be enclosed in quotes.
- json[@Value(index)] – This path refers to the item in the root array at the position indicated by the ‘index’ attribute.
- json["@Value(key)"] – This path refers to the object value whose key is named in the ‘key’ attribute.
Note: The @Value() functions used in the path parameter will apply to the Update feature being processed, not the Document feature that is being updated
This parameter is used to specify the specific object key / array index whose value is to be inserted/removed/modified.
For object updates, this value of this parameter should be a string, which will be interpreted as an object key. Quotes are NOT required around the string in this parameter.
For array updates, the value of this parameter should be an integer. Note that array indices start at 0.
This parameter is used to indicate how the value of the Value parameter should be interpreted. The effects of the different values of this parameter are explained in the description of the Value parameter. In general, this parameter should be set to Plain Text if the transformer is intended to insert text content into the document, while it should be set to JSON/XQuery if the transformer is intended to insert JSON objects or arrays into the document, or if the value is being produced through XQuery function calls.
This parameter is used to specify the new values that should be inserted into the document. If the Value Type parameter is set to Plain Text, the contents of this parameter will be encoded for safe use in an XML document. That is, double quotes will be escaped as \", backslashes will be escaped as \\, and so on.
If the Value Type parameter is set to JSON/XQuery, the contents of this parameter will be interpreted as an XQuery expression. This allows the insertion of JSON values elements into the document. Also, this allows the use of the FME XQuery functions. For example, a JSON Template (for example, from the JSONTemplater transformer) could be entered as the parameter value. It would then be evaluated before being inserted into the document.
Note: Any FME XQuery functions used in the new value will access the Update feature, not the Document feature.
Parameters
This parameter specifies whether or not Workbench should try to validate the attribute names which are passed to any XQuery functions used in the Value column.
This parameter specifies the attribute to which the updated JSON document will be written. The default is _result.
Editing Transformer Parameters
Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.
Defining Values
There are several ways to define a value for use in a Transformer. The simplest is to simply type in a value or string, which can include functions of various types such as attribute references, math and string functions, and workspace parameters. There are a number of tools and shortcuts that can assist in constructing values, generally available from the drop-down context menu adjacent to the value field.
Using the Text Editor
The Text Editor provides a convenient way to construct text strings (including regular expressions) from various data sources, such as attributes, parameters, and constants, where the result is used directly inside a parameter.
Using the Arithmetic Editor
The Arithmetic Editor provides a convenient way to construct math expressions from various data sources, such as attributes, parameters, and feature functions, where the result is used directly inside a parameter.
Conditional Values
Set values depending on one or more test conditions that either pass or fail.
Parameter Condition Definition Dialog
Content
Expressions and strings can include a number of functions, characters, parameters, and more.
When setting values - whether entered directly in a parameter or constructed using one of the editors - strings and expressions containing String, Math, Date/Time or FME Feature Functions will have those functions evaluated. Therefore, the names of these functions (in the form @<function_name>) should not be used as literal string values.
These functions manipulate and format strings. | |
Special Characters |
A set of control characters is available in the Text Editor. |
Math functions are available in both editors. | |
Date/Time Functions | Date and time functions are available in the Text Editor. |
These operators are available in the Arithmetic Editor. | |
These return primarily feature-specific values. | |
FME and workspace-specific parameters may be used. | |
Creating and Modifying User Parameters | Create your own editable parameters. |
Dialog Options - Tables
Transformers with table-style parameters have additional tools for populating and manipulating values.
Row Reordering
|
Enabled once you have clicked on a row item. Choices include:
|
Cut, Copy, and Paste
|
Enabled once you have clicked on a row item. Choices include:
Cut, copy, and paste may be used within a transformer, or between transformers. |
Filter
|
Start typing a string, and the matrix will only display rows matching those characters. Searches all columns. This only affects the display of attributes within the transformer - it does not alter which attributes are output. |
Import
|
Import populates the table with a set of new attributes read from a dataset. Specific application varies between transformers. |
Reset/Refresh
|
Generally resets the table to its initial state, and may provide additional options to remove invalid entries. Behavior varies between transformers. |
Note: Not all tools are available in all transformers.
FME Licensing Level
FME Professional edition and above
FME Community
The FME Community is the place for demos, how-tos, articles, FAQs, and more. Get answers to your questions, learn from other users, and suggest, vote, and comment on new features.
Search for samples and information about this transformer on the FME Community.