Skip to main content

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": r
    {
      "name": "_portalLayer",
      "value": "parks"
    },
    {
      "name": "_portalOrganisation",
      "value": "SWR"
    }
  ]
}

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!


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

rAddition] 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" : $valuesv$i]
}
]
}

 


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