Solved

outputting a numeric array using JSON templater


Hi There,

I am using the JSON templater to create various JSON outputs. One of them is them is "Position" which consists of two decimal / float values - XCOORD and YCOORD. I can use either of the following to output the values:

"POSITION":[fme:get-attribute("X_COORD"),fme:get-attribute("Y_COORD")] OR

"POSITION":[fme:get-list-attribute("_XY_LIST{}.XY_List_Creator")] (this is a list I have created with the co-ord pair)

The issue is, that my JSON schema expects the "POSITION" value to be a numeric JSON array, but no matter how I write out the XCOORD and YCOORD values, it always appears as a string eg.

"POSITION" : [ "515000.00", "188000.00" ]

I've made sure the individual values are indeed numeric. Right before the templator they are coded as UTF-8 - not sure what it is doing to convert it to a string.

i've even tried using a Math function to convert in line but with no luck:

"POSITION":[@double(fme:get-attribute("X_COORD")),@double(fme:get-attribute("Y_COORD"))],

 

Any help would be much appreciated!

Thanks,

 

Luke

icon

Best answer by david_r 16 May 2019, 09:53

View original

4 replies

Userlevel 3
Badge +17

Hi @lukestudd

If the two attributes are appearing as UTF-8 before the JSONTemplater, then FME thinks they are strings, instead of numbers. There may be some whitespace that is affecting how FME parses the attribute value.

You can trying using one of the Math functions in the Text Editor force it to a number in an AttributeManager before the JSONTemplater.

Userlevel 4

You need to cast the strings to floats using XQuery casts. 

Given the following list:

_list{}.X_COORD
_list{}.Y_COORD

you can use the following expression:

{
    "POSITION" : [
        let $xv := fme:get-list-attribute("_list{}.X_COORD")
        let $yv := fme:get-list-attribute("_list{}.Y_COORD")
        for $x at $n in $xv
            return { xs:double($x), xs:double($yv[$n])}
    ]
}

Sample result:

{ "POSITION" : [ 12.1, 1.12345 ] }

You need to cast the strings to floats using XQuery casts. 

Given the following list:

_list{}.X_COORD
_list{}.Y_COORD

you can use the following expression:

{
    "POSITION" : [
        let $xv := fme:get-list-attribute("_list{}.X_COORD")
        let $yv := fme:get-list-attribute("_list{}.Y_COORD")
        for $x at $n in $xv
            return { xs:double($x), xs:double($yv[$n])}
    ]
}

Sample result:

{ "POSITION" : [ 12.1, 1.12345 ] }

This worked an absolute treat - thank you @david_r! Much appreciated.

Badge +4

Hi @lukestudd

If the two attributes are appearing as UTF-8 before the JSONTemplater, then FME thinks they are strings, instead of numbers. There may be some whitespace that is affecting how FME parses the attribute value.

You can trying using one of the Math functions in the Text Editor force it to a number in an AttributeManager before the JSONTemplater.

Is there a way to strip the quotes wrapping an array (not the values inside the array)?

Reply