Skip to main content
Solved

How can I convert an attribute from string to number? I want to write to a JSON file and have them in a number format.


katt
Supporter
Forum|alt.badge.img+12
  • Supporter

Hello, I am reading some attributes from a database. Some are in string format, some in number (int, float etc.). I want to write these attributes to a json file.I noticed that all the attributes in string format are automatically output in quotation marks and the numbers without. In my case I have three attributes: district, lat and long. District is an int, lat and long are strings. How can I change lat and long so that their output is also without quotation marks like the district one (see image) ?

Best answer by ebygomm

katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

You can do something like this

{
 
"district" : xs:int(fme:get-attribute("district")),
"lat": xs:float(fme:get-attribute("lat")),
"lon": xs:float(fme:get-attribute("lon"))
 
}

 

 

View original
Did this help you find an answer to your question?

13 replies

carmijo
Supporter
Forum|alt.badge.img+11
  • Supporter
  • October 20, 2022

Hi @katt, you can use the stringformatter transformer. Use one of these options that meets your needs.

image--

If i've provided something helpful, please upvote the comment. If i've provided a solution, please mark it as 'Best Answer'. This helps the community find solutions when your question is referenced.

If I didn't provide a solution, please let me know if I can help further. Thanks! 🙂

 


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • October 20, 2022
carmijo wrote:

Hi @katt, you can use the stringformatter transformer. Use one of these options that meets your needs.

image--

If i've provided something helpful, please upvote the comment. If i've provided a solution, please mark it as 'Best Answer'. This helps the community find solutions when your question is referenced.

If I didn't provide a solution, please let me know if I can help further. Thanks! 🙂

 

thank you @carmijo​ . I have already tried this. I used .2f in the string formatter and look at the output. It is still a string and not a float even though it has been reduced to two decimals.


carmijo
Supporter
Forum|alt.badge.img+11
  • Supporter
  • October 20, 2022
katt wrote:

thank you @carmijo​ . I have already tried this. I used .2f in the string formatter and look at the output. It is still a string and not a float even though it has been reduced to two decimals.

That's strange. have you tried running it through a second stringformatter with 'f' only?


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • October 20, 2022
katt wrote:

thank you @carmijo​ . I have already tried this. I used .2f in the string formatter and look at the output. It is still a string and not a float even though it has been reduced to two decimals.

I just did it. No change in format though.


carmijo
Supporter
Forum|alt.badge.img+11
  • Supporter
  • October 20, 2022
katt wrote:

thank you @carmijo​ . I have already tried this. I used .2f in the string formatter and look at the output. It is still a string and not a float even though it has been reduced to two decimals.

very strange. from what i see, you're doing everything right. sorry i can't help further. might be worth opening a case with safe


ebygomm
Influencer
Forum|alt.badge.img+33
  • Influencer
  • October 20, 2022

How are you creating your json file?


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • October 20, 2022
ebygomm wrote:

How are you creating your json file?

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!


ebygomm
Influencer
Forum|alt.badge.img+33
  • Influencer
  • Best Answer
  • October 20, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

You can do something like this

{
 
"district" : xs:int(fme:get-attribute("district")),
"lat": xs:float(fme:get-attribute("lat")),
"lon": xs:float(fme:get-attribute("lon"))
 
}

 

 


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • October 20, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

Great. Thanks. I tried and it is the result I was looking for! are you good at datetime conversion as well ? I have it in this format "TIMESTAMP (string: UTF-8): 20221017174751.999" and I want the output to look like this format in the json:"timestamp": "2021-11-10T15:37:30+00:00". I used the DateTimeConverter but any parameters I was using, it is always rejected. Maybe you can help me out with this one as well?


ebygomm
Influencer
Forum|alt.badge.img+33
  • Influencer
  • October 20, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

You need to set the TimeZone and then convert 

e.g.

@DateTimeFormat(@TimeZoneSet(@Value(date),+00:00),%Y-%m-%dT%H:%M:%S%z)

 


Forum|alt.badge.img+2
  • October 20, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

@katt​ it's usually best to start a new question for a new topic. But this should work:imageMake sure your selecting an attribute (ellipsis) not an Attribute Value. You'll have to add the UTC Offset manually


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • October 20, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

This works great. Thanks again.


katt
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • November 18, 2022
katt wrote:

I am using a JSON Templater. Maybe there is workaround to display the lat and long values without quotation marks?!

Hello @ebygomm​ , how can I limit the number of decimals to 4 ? In my case I only want 4 decimals after the point. If I use xs:float it adds some zeros sometimes, but I am not sure how I can limit the decimals numbers ? thank you.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings