Skip to main content
Solved

JSONTemplater casting arrays

  • November 8, 2021
  • 4 replies
  • 47 views

jdh
Contributor
Forum|alt.badge.img+37
  • Contributor
  • 2002 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]}

 

 

Best answer by ebygomm

Something like this?

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

 

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

hkingsbury
Celebrity
Forum|alt.badge.img+63
  • Celebrity
  • 1625 replies
  • November 9, 2021

Could do something janky like this with regex?

image


virtualcitymatt
Celebrity
Forum|alt.badge.img+47
  • Celebrity
  • 2000 replies
  • November 9, 2021

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


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • Best Answer
  • November 9, 2021

Something like this?

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

 


david_r
Celebrity
  • 8392 replies
  • November 9, 2021

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