Skip to main content
Solved

how to dynamically create a json

  • May 12, 2017
  • 5 replies
  • 165 views

arthy
Contributor
Forum|alt.badge.img+8

Hello,

I implemented a changed detection on oracle table.

Now I would like to be able to create a json for each change per object.

 For instance, for an object where changes were detected on 5 attributs, below will be the json that I would like to obtain:

{
"ATTRIBUT1": {
"OLD_VALUE": "OLD1",
"NEW_VALUE": "NEW1"
},
"ATTRIBUT2": {
"OLD_VALUE": "OLD2",
"NEW_VALUE": "NEW2"
},
"ATTRIBUT3": {
"OLD_VALUE": "OLD3",
"NEW_VALUE": "NEW3"
},
"ATTRIBUT4": {
"OLD_VALUE": "OLD4",
"NEW_VALUE": "NEW4"
},
"ATTRIBUT5": {
"OLD_VALUE": "OLD5",
"NEW_VALUE": "NEW5"
}
}

How can I proceed to obtain this in fme?

I was tinking of the agregator to group all the changes from the same object (based on an objectid) and then build the json but I don't know how to proceed.

Please keep in mind that the number  and names of attributes will change from one object to another one.

Thanks,

Best answer by jdh

If I understand correctly, you have a separate feature for each change.

 

You can use the JSONTemplater, with a subtemplate to build your desired JSON.

see http://docs.safe.com/fme/2017.0/html/FME_Desktop_Documentation/FME_Transformers/Transformers/jsontemplater.htm, particularly the section "Selecting the Sub-Features to Process"

I would use a sampler to get the first feature for each objectID to the Root, and sent it as well as all the other features to the subtemplate.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • Best Answer
  • May 12, 2017

If I understand correctly, you have a separate feature for each change.

 

You can use the JSONTemplater, with a subtemplate to build your desired JSON.

see http://docs.safe.com/fme/2017.0/html/FME_Desktop_Documentation/FME_Transformers/Transformers/jsontemplater.htm, particularly the section "Selecting the Sub-Features to Process"

I would use a sampler to get the first feature for each objectID to the Root, and sent it as well as all the other features to the subtemplate.


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • May 12, 2017

If I understand correctly, you have a separate feature for each change.

 

You can use the JSONTemplater, with a subtemplate to build your desired JSON.

see http://docs.safe.com/fme/2017.0/html/FME_Desktop_Documentation/FME_Transformers/Transformers/jsontemplater.htm, particularly the section "Selecting the Sub-Features to Process"

I would use a sampler to get the first feature for each objectID to the Root, and sent it as well as all the other features to the subtemplate.

@jdh,

 

Using the JSonTemplater and grouping on the objectifid as you said, I'm having having 5 json file but what I want it is only one json file.

 

 

Yes a change can only be made on one feature at a time. However, a change can affect several attribut of the same feature. In the example described above, we are having 5 attributes that were changes on the same feature

 


takashi
Celebrity
  • May 13, 2017

Hi @arthy, probably the JSONTemplater would be a solution, but template expression depends on how the feature contains the relevant attributes. What attributes contain the values which should be written into the destination JSON document?


jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • May 15, 2017
@jdh,

 

Using the JSonTemplater and grouping on the objectifid as you said, I'm having having 5 json file but what I want it is only one json file.

 

 

Yes a change can only be made on one feature at a time. However, a change can affect several attribut of the same feature. In the example described above, we are having 5 attributes that were changes on the same feature

 

Try a subtemplate of

 

{fme:get-attribute("Attribute"): {"OLD_VALUE": fme:get-attribute("OldValue"), "NEW_VALUE":fme:get-attribute("NewValue")}}

 

 

and in the root template

 

{fme:process-features("SUB", "ObjectID", fme:get-attribute("ObjectID"))}

 

 

or if you want the object id reference in the json object, something like

 

{"ObjectID" : fme:get-attribute("ObjectID"), "Changes" : {fme:process-features("SUB", "ObjectID", fme:get-attribute("ObjectID"))}}

 

 

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • May 22, 2017
Try a subtemplate of

 

{fme:get-attribute("Attribute"): {"OLD_VALUE": fme:get-attribute("OldValue"), "NEW_VALUE":fme:get-attribute("NewValue")}}

 

 

and in the root template

 

{fme:process-features("SUB", "ObjectID", fme:get-attribute("ObjectID"))}

 

 

or if you want the object id reference in the json object, something like

 

{"ObjectID" : fme:get-attribute("ObjectID"), "Changes" : {fme:process-features("SUB", "ObjectID", fme:get-attribute("ObjectID"))}}

 

 

 

@jdh,

 

I tried it and it works perfectly.

 

Thanks