Solved

JSONTemplater casting arrays

  • 8 November 2021
  • 4 replies
  • 2 views

Badge +22
  • Contributor
  • 1959 replies

In the JSONTemplater is there a way to cast an array of strings into an array of intergers?

 

https://community.safe.com/s/question/0D54Q000080hA0SSAU/how-to-coerce-attribute-type-for-jsontemplater?t=1636407693748  answers the question for regular attributes   but this does not work with fme:get-list-attribute.

 

{"ids": [(fme:get-list-attribute("_list{}.id"))]
 }

returns {"ids": ["1","2","3"]}    

I would like

{"ids": [1,2,3]}

 

 

icon

Best answer by ebygomm 9 November 2021, 10:28

View original

4 replies

Userlevel 5
Badge +29

Could do something janky like this with regex?

image

Userlevel 4
Badge +26

I used something like this in my JOSNTemplator:

  "errors": [
    for $v in fme:get-list-attribute('errors{}.error_code')
    return xs:integer($v)
  ]

This taken from an old post of Takashi's - no credit to me

Badge +10

Something like this?

{
    "ids" : [
        let $list := fme:get-list-attribute("_list{}.id")
        for $x at $n in $list
            return { xs:int($x)}
    ]
}

 

Userlevel 4

Try the following:

{"ids": [
        let $ids := fme:get-list-attribute("_list{}.id")
        for $x in $ids
            return {xs:int($x)}
    ]
}

See also: https://community.safe.com/s/question/0D54Q000080hDRQSA2/outputting-a-numeric-array-using-json-templater

Reply