Question

How to transform and group features into JSON output format

  • 29 December 2018
  • 2 replies
  • 8 views

Badge

I have these features that I would like to transform and group into JSON output:

package_idpackage_nameresource_nameresource_url1Package AResource 1http://www.resource-A1.com1Package AResource 2http://www.resource-A2.com1Package AResource 3http://www.resource-A3.com2Package BResource 1http://www.resource-B1.com2Package BResource 2http://www.resource-B2.com3Package CResource 1http://www.resource-C1.com3Package CResource 2http://www.resource-C2.com3Package CResource 3http://www.resource-C3.com4Package DResource 1http://www.resource-D1.com

 

The resulting dataset should consist of 4 features, one feature for each package.

 

For each feature or package I need an attribute containing the package data in JSON format.

 

For example the value of the JSON attribute for first feature should be:
{
  "id" : "1", 
  "name" : "Package A",
  "resources" : [
                  {"name" : "Resource 1", "url" : "http://www.resource-A1.com"},
                  {"name" : "Resource 2", "url" : "http://www.resource-A2.com"},
                  {"name" : "Resource 3", "url" : "http://www.resource-A3.com"},
                ]
}

For the second one:

{
  "id" : "2", 
  "name" : "Package B",
  "resources" : [
                  {"name" : "Resource 1", "url" : "http://www.resource-B1.com"},
                  {"name" : "Resource 2", "url" : "http://www.resource-B2.com"}
                ]
}

I think I should probably use the JSONTemplater transformer, combining a root template with a sub template, but I cannot seem to figure out how it is done. Any help would be greatly appreciated.


2 replies

Userlevel 2
Badge +17

You are on the right track. This workflow and template expressions might help you.

0684Q00000ArLVTQA3.png

Root:

{
    "id" : fme:get-attribute("package_id"),
    "name": fme:get-attribute("package_name"),
    "resources" [
        fme:process-features("SUB")
    ]
}

SUB:

{
    "name" : fme:get-attribute("resource_name"),
    "url" : fme:get-attribute("resource_url")
}
Badge

You are on the right track. This workflow and template expressions might help you.

0684Q00000ArLVTQA3.png

Root:

{
    "id" : fme:get-attribute("package_id"),
    "name": fme:get-attribute("package_name"),
    "resources" [
        fme:process-features("SUB")
    ]
}

SUB:

{
    "name" : fme:get-attribute("resource_name"),
    "url" : fme:get-attribute("resource_url")
}

Thank you very much! Exactly what I needed.

Reply