Skip to main content
Solved

outputting a numeric array using JSON templater

  • May 15, 2019
  • 4 replies
  • 192 views

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

Best answer by david_r

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 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.

4 replies

debbiatsafe
Safer
Forum|alt.badge.img+21
  • Safer
  • 648 replies
  • May 16, 2019

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.


david_r
Celebrity
  • 8392 replies
  • Best Answer
  • May 16, 2019

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

  • Author
  • 1 reply
  • May 16, 2019

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.


jnotter
Contributor
Forum|alt.badge.img+5
  • Contributor
  • 18 replies
  • February 6, 2020

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)?