Skip to main content
Question

How to transform and group features into JSON output format

  • December 29, 2018
  • 2 replies
  • 142 views

friesewoudloper
Participant
Forum|alt.badge.img+1

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.

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.

2 replies

takashi
Celebrity
  • 7843 replies
  • December 30, 2018

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")
}

friesewoudloper
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • 50 replies
  • January 2, 2019

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.