Quote Numbers in Json Stream
Hello Community,
My Intersystems Caché Version: 1.2014 (Can't update now.)
I have the following issue:
I have for example an articlenumber with 15049950, which is numeric. But sometimes it can also be an alphanumeric string like PK15049950.
How can i set numbers always to string in Json Stream with quotes like "15049950".
Code Example:
set object = ##class(%ZEN.proxyObject).%New()
set articlenumber = "15049950"
set object.articlenumber = articlenumber
set x = ##class(%ZEN.Auxiliary.jsonArrayProvider).%WriteJSONStreamFromObject(.json,object)
Will output:
{
"articlenumber": 15049950
}
but I want always:
{
"articlenumber": "15049950"
}
Thank you and best regards!
Comments
This is pretty old version of Cache.
Check the alternative JSON library Planet Cache - HTH.
Through %ZEN.proxyObject is unlikely to work, since the q parameter cannot be disabled in this case
q - output numeric values unquoted even when they come from a non-numeric property
Use your own class, for example:
Class dc.test Extends %RegisteredObject
{
Property articlenumber As %String;
}s object = ##class(dc.test).%New() s object.articlenumber = "15049950" s x = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.json,object,,,,"aelotw")
Output:
{
"articlenumber":"15049950"
}Hey Vitaliy,
It works! Very smart solution to create your own custom class.
You helped me so much, thank you!
Best regards.
I have below issue -
I have an ID with 123456.(ID as a dynamic value coming from request message)
I wanted it to be displayed as "123456" in Json Stream.
Code Example:
set object = ##class(%ZEN.proxyObject).%New()
set object.ID = ID
set x = ##class(%ZEN.Auxiliary.jsonArrayProvider).%WriteJSONStreamFromObject(.json,object)
Case:1
ID : 123456
Will output:
{
"ID": 123456
}
but I need :
{
"ID": "123456"
}
Case:2
ID : "123456" (adding double quotes)
Will output:
{
"ID": "\"123456\""
}
but I need :
{
"ID": "123456"
}
Thanks in advance.
Through %ZEN.proxyObject is unlikely to work, since the q parameter cannot be disabled in this case
q - output numeric values unquoted even when they come from a non-numeric property
Use your own class, for example:
Class dc.proxyObject Extends %RegisteredObject
{
Property ID As %VarString;
}set object = ##class(dc.proxyObject).%New() set object.ID = 123456 set x = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.json,object,,,,"aelotw")
Output:
{
"ID":"123456"
}