Skip to main content
Solved

JSON Templator Correct Syntax

  • May 5, 2025
  • 3 replies
  • 64 views

deanhowell
Influencer
Forum|alt.badge.img+24

I need to output a list of paired values as a JSON File.

I have a list called JSONList with two attributes JSONname and JSONvalue.

I am using the JSONTemplater to organise the data.

I have the root template set as

{
  "publishedParameters": [
    fme:process-features("SUB")
  ]
}

and the SUB Template as

{
  "name": fme:get-list-attribute("JSONList{}.JSONname"),
  "value": fme:get-list-attribute("JSONList{}.JSONvalue")
}
 

but I am just getting the name and value as a single line each.

 

 

How do I change the structure so each name and value pair are separated, similar to the below?

 

{
  "publishedParameters": [
    {
      "name": "_portalLayer",
      "value": "parks"
    },
    {
      "name": "_portalOrganisation",
      "value": "SWR"
    }
  ]
}

Best answer by takashi

Hi ​@deanhowell ,

A simple way is to extract elements of the JSONList{} with ListExploder beforehand, then set this expression as the SUB template in the JSONTemplator.

{
"name": fme:get-attribute("JSONname"),
"value": fme:get-attribute("JSONvalue")
}

[Addition] Alternatively, using XQuery syntax, this Root template expression is also possible, without using SUB expression.

{
"publishedParameters" :
[
let $names := fme:get-list-attribute("JSONList{}.JSONname")
let $values := fme:get-list-attribute("JSONList{}.JSONvalue")
for $i in (1 to count($names))
return
{
"name" : $names[$i],
"value" : $values[$i]
}
]
}

 

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.

3 replies

crutledge
Influencer
Forum|alt.badge.img+43
  • Influencer
  • May 5, 2025

Hi ​@deanhowell 

I am curious how to do this as well. Seems like it is taking the whole list and writing out as one line where we want it create a record set of pairs. if we did this by hand we would do something like:

{
  "name": fme:get-list-attribute("JSONList{0}.JSONname"),
  "value": fme:get-list-attribute("JSONList{0}.JSONvalue")
}
{
  "name": fme:get-list-attribute("JSONList{1}.JSONname"),
  "value": fme:get-list-attribute("JSONList{1}.JSONvalue")
}

 

But we want it to iterate through the list like:

{
  "name": fme:get-list-attribute("JSONList{n}.JSONname"),
  "value": fme:get-list-attribute("JSONList{n}.JSONvalue")
}

 

I am sure there is a simple straight forward solution. I will share if I get something working. Good luck!


takashi
Celebrity
  • Best Answer
  • May 5, 2025

Hi ​@deanhowell ,

A simple way is to extract elements of the JSONList{} with ListExploder beforehand, then set this expression as the SUB template in the JSONTemplator.

{
"name": fme:get-attribute("JSONname"),
"value": fme:get-attribute("JSONvalue")
}

[Addition] Alternatively, using XQuery syntax, this Root template expression is also possible, without using SUB expression.

{
"publishedParameters" :
[
let $names := fme:get-list-attribute("JSONList{}.JSONname")
let $values := fme:get-list-attribute("JSONList{}.JSONvalue")
for $i in (1 to count($names))
return
{
"name" : $names[$i],
"value" : $values[$i]
}
]
}

 


deanhowell
Influencer
Forum|alt.badge.img+24
  • Author
  • Influencer
  • May 5, 2025

Thanks ​@takashi great advice as always. I was just over complicating things and had a list builder in the process, which wasn’t needed.