Skip to main content
So i have been going through tutorials and JSON documents on safe.com and I cannot figure out how to bring out the attributes I need from this for my workbench. I am getting this http call in json format and I want to get this down to working with these attributes

 

 

action, custom_1, height, class, MATL_TYPE , F_ID, POLETYPE, F_ROUTE, F_O, F_Y, EMINY, EMINX, DATABASE_NAME, DESIGNATION, objectType

 

 

I cannot figure out the procedure to be able to pull these attributes out and their values to work with in my workbench. Any help would be appreciated. The original message is below.

 

 

MESSAGE:

 

 

{"action":"CreateOrUpdate","instance":{"fields":s{"allowNull":false,"allowUpdate":false,"targetField":"custom_1","value":"5b8e3770-cf35"},{"allowNull":false,"allowUpdate":false,"targetField":"height","value":"2"},{"allowNull":false,"allowUpdate":false,"targetField":"class","value":"2"},{"allowNull":false,"allowUpdate":false,"targetField":"MATL_TYPE","value":"plastic"},{"allowNull":false,"allowUpdate":false,"targetField":"lighttype","value":"JOEC-jointowned"},{"allowNull":false,"allowUpdate":false,"targetField":"F_ID","value":"1988"},{"allowNull":false,"allowUpdate":false,"targetField":"F_ROUTE","value":"MIDDLE"},{"allowNull":false,"allowUpdate":false,"targetField":"F_O","value":""},{"allowNull":false,"allowUpdate":false,"targetField":"F_Y","value":"2015"},{"allowNull":false,"allowUpdate":false,"targetField":"EMINY","value":"71.34"},{"allowNull":false,"allowUpdate":false,"targetField":"EMINX","value":"-71.86"},{"allowNull":false,"allowUpdate":false,"targetField":"ENAME","value":"0:57:0:2"},{"allowNull":false,"allowUpdate":false,"targetField":"DATABASE_NAME","value":"Main"},{"allowNull":false,"allowUpdate":false,"targetField":"DESIGNATION","value":"JOINTOWNED"},{"allowNull":false,"allowUpdate":false,"targetField":null,"value":null}]},"objectType":"pole","xmlns":"http:\\/\\/www.xxx/version_2.0","xmlns_xsd":"http:\\/\\/www.xx.org\\/2001\\/XMLSchema","xmlns_xsi":"http:\\/\\/www.xx.org\\/2001\\/XMLSchema-instance"}

 

 

 

I have did orginaly take this and take out everything but the attributes i needed and the values and was easily able to use a JSONfragmenter transformer but cannot figure out how to deal with the message in a whole and it is to much work to always break it down. the plan is to publish on fme server and automate the intake of messages so need to figure out how to get this to work.

 

 

Hi,

 

 

If you flatten the JSON document with the JSONFlattener, the list elements in the document will be stored by a structured list called "instance.fields{}", which consists of "instance.fields{}.targetField" (field names) and "instance.fields{}.value" (field values). You can then convert each field value element of the "instance.fields{}.value" into a regular attribute with corresponding field name. For example:

 

  1. JSONFlattener: Flatten the JSON document, and expose "instance.fields{}.targetField" and "instance.fields{}.value".
  2. ListExploder: Explode the feature on "instance.fields{}". Output features will have two attributes - "targetField" and "value".
  3. AttributeCreator: Create a new attribute that stores the field value for each exploded feature. Set the value of "targetField" to the new attribute name, set the value of "value" to the new attribute value.
  4. Aggregator: Aggregate the features (Keep Input Attributes: Yes).
  5. AttributeExposer: Expose all the required attribute names - "action", "custom_1", "height", etc.
Note: If you have two or more JSON documents to be processed at the same time, use a Counter to add temporary ID to each feature before exploding and set the ID attribute to the "Group By" parameter of the Aggregator.

 

 

Alternatively, if you are familiar with scripting, you can replace 2. ListExploder - 5. AttributeExposer with a PythonCaller or TclCaller. This is a Tcl script example for the TclCaller.

 

proc renameListElements {} {
    foreach attr iFME_AttributeNames] {
        if {tregexp {^(instance\\.fields\\{\d+\\})\\.targetField$} $attr m prefix]} {
            FME_RenameAttribute >FME_GetAttribute $attr] Âformat "%s.value" $prefix]
        }
    }
}
Takashi
Thank you so much. This is amazing
Hi,

 

 

If you flatten the JSON document with the JSONFlattener, the list elements in the document will be stored by a structured list called "instance.fields{}", which consists of "instance.fields{}.targetField" (field names) and "instance.fields{}.value" (field values). You can then convert each field value element of the "instance.fields{}.value" into a regular attribute with corresponding field name. For example:

 

  1. JSONFlattener: Flatten the JSON document, and expose "instance.fields{}.targetField" and "instance.fields{}.value".
  2. ListExploder: Explode the feature on "instance.fields{}". Output features will have two attributes - "targetField" and "value".
  3. AttributeCreator: Create a new attribute that stores the field value for each exploded feature. Set the value of "targetField" to the new attribute name, set the value of "value" to the new attribute value.
  4. Aggregator: Aggregate the features (Keep Input Attributes: Yes).
  5. AttributeExposer: Expose all the required attribute names - "action", "custom_1", "height", etc.
Note: If you have two or more JSON documents to be processed at the same time, use a Counter to add temporary ID to each feature before exploding and set the ID attribute to the "Group By" parameter of the Aggregator.

 

 

Alternatively, if you are familiar with scripting, you can replace 2. ListExploder - 5. AttributeExposer with a PythonCaller or TclCaller. This is a Tcl script example for the TclCaller.

 

proc renameListElements {} {
    foreach attr iFME_AttributeNames] {
        if {tregexp {^(instance\\.fields\\{\d+\\})\\.targetField$} $attr m prefix]} {
            FME_RenameAttribute >FME_GetAttribute $attr] Âformat "%s.value" $prefix]
        }
    }
}
Takashi

I was wondering if you could help out just a little more. I really appreciate what you have helped with so far and I can see the finish line. Where I am having issues is getting the attributes (custom_1, F_O, ext) to populate. I know I am messing up in either step 3 or 4.

 

 

Step 3: attribute creator I am creating two attributes:

 

 

attributes with a value of targetfield

 

values with a value of value

 

 

I left all other parameters as the base default

 

 

step 4: aggregator

 

 

Input is ordered by a group: yes

 

Parameters: mode = attributes only

 

 

attribute accumulation

 

keep input attributes = yes

 

list name = attributes

 

left all others no items selected

 

 

I am getting results but not getting the attributes to match their values.

I was wondering if you could help out just a little more. I really appreciate what you have helped with so far and I can see the finish line. Where I am having issues is getting the attributes (custom_1, F_O, ext) to populate. I know I am messing up in either step 3 or 4.

 

 

Step 3: attribute creator I am creating two attributes:

 

 

attributes with a value of targetfield

 

values with a value of value

 

 

I left all other parameters as the base default

 

 

step 4: aggregator

 

 

Input is ordered by a group: yes

 

Parameters: mode = attributes only

 

 

attribute accumulation

 

keep input attributes = yes

 

list name = attributes

 

left all others no items selected

 

 

I am getting results but not getting the attributes to match their values.

Setp 3: The AttributeCreator has to create ONE attribute storing the value of "value", and its name should be the VALUE of "targetField".

Step 4: You don't need to create list with the Aggregator. Just aggregate the features, each of which has an attribute containing the field value with corresponding field name. The attribute is the one that has been created by the AttributeCreator in Step 3.


Setp 3: The AttributeCreator has to create ONE attribute storing the value of "value", and its name should be the VALUE of "targetField".

Step 4: You don't need to create list with the Aggregator. Just aggregate the features, each of which has an attribute containing the field value with corresponding field name. The attribute is the one that has been created by the AttributeCreator in Step 3.

Thanks, this is amazing. I am def gonna follow your posts. You seem to know all the tricks.


Reply