Skip to main content
Solved

JSON Templator Correct Syntax


deanhowell
Influencer
Forum|alt.badge.img+23

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]
        }
    ]
}

 

View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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+32
  • 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
Influencer
  • 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+23
  • 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.

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings