Question
· May 13

Issue in generating Double quotes for a property in Json Stream.

Hello Community,

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.

Discussion (3)1
Log in or sign up to continue

If your system does not support JSON (i.e. pre 2016.2?) then give this "dirty trick" a try:

- add a zero-width-space character to your numbers
- create the output stream
- remove the zero-width-space characters

Instead of the zero-width-space you can use any other character too, which does not appear in your data (binary data should be base64 encoded).

ClassMethod WithQuotes()
{
	set zwsp = $c(8203) // zero-width-space
	set obj = ##class(%ZEN.proxyObject).%New()
	
	set obj.ID = 1234_zwsp
	set obj.Number=123.45_zwsp
	
	if ##class(%ZEN.Auxiliary.jsonArrayProvider).%WriteJSONStreamFromObject(.tmp,obj) {
  		set json=##class(%Stream.TmpBinary).%New()
		do tmp.Rewind()
		while 'tmp.AtEnd { do json.Write($tr(tmp.Read(32000),zwsp)) }
	}
	
	do json.Rewind()
	write json.Read(json.Size)
}