Store property coming from a json array
Hi,
I know there are several alternatives, but I would like to find the easiest & simpler ones to store data coming in Json format from post requests and also allowing me to do SQL queries.
I want to have a property called favouriteColors. I want to store a few colors, and I want to be able to do queries to get top favorite colors, etc... so not handling the list of colors as just a fixed string or fixed object.
If I want to store this information, I have several alternatives, like %DynamicArray, list of %String o maybe just %String, but I want to find the best way to store the property in a way that I can do SQL queries like
select COUNT(colors %FOREACH FavoriteColors) from Table
And an easy way to get the object from HTTP:
FavouriteColors = ["green","yellow","blue"]
and save it.
My initial idea is
FavouriteColors as list of %String
But then, the way to save from JSON received via POST doesn't like me. I did something like
d ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(valueRecived.%ToJSON(),,.list)
Set table.FavouriteColors = listThis one doesn't like me that much as I am using %ZEN.Auxiliary.
Comments
Hello @Mario Sanchez Macias ,
I think it's not a correct usage of %ConvertJSONToObject, the third argument must be a target object instance.
Depending on your need, you should use "%Array of %String". It's more flexible for SQL that "%List".
If "valueRecived" variable is a dynamic array like ["green","yellow","blue"], you can test this code :
Set valueRecived = ["green","yellow","blue"]
Set array = ##class(%ArrayOfDataTypes).%New()
Do ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(valueRecived.%ToJSON(),,.array)
Zw arrayNo need to init the array object:
Set valueRecived = ["green","yellow","blue"]
Do ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(valueRecived.%ToJSON(),,.array)
Zw array
So you didn't find another simpler alternatives to %ConvertJSONToObject from the %ZEN package?
I will report to my colleagues from development, as not sure using %ZEN is the best way to do this.
Thanks
Mario
Simple example:
<FONT COLOR="#000080">Class dc.test Extends </FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#000080">%RegisteredObject</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#000080">%JSON.Adaptor</FONT><FONT COLOR="#000000">)
{
</FONT><FONT COLOR="#000080">Property </FONT><FONT COLOR="#000000">list </FONT><FONT COLOR="#000080">As list Of %String</FONT><FONT COLOR="#000000">;
</FONT><FONT COLOR="#000080">/// d ##class(dc.test).test()
ClassMethod </FONT><FONT COLOR="#000000">test()
{
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">json</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#ff00ff">{</FONT><FONT COLOR="#008000">"list"</FONT><FONT COLOR="#808080">:</FONT><FONT COLOR="#ff00ff">[</FONT><FONT COLOR="#008000">"green"</FONT><FONT COLOR="#808080">,</FONT><FONT COLOR="#008000">"yellow,red"</FONT><FONT COLOR="#808080">,</FONT><FONT COLOR="#008000">"blue"</FONT><FONT COLOR="#ff00ff">]}
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">=..</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%JSONImport</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">json</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">list</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Count</FONT><FONT COLOR="#000000">(),!,</FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">list</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">GetAt</FONT><FONT COLOR="#000000">(2)
}
}</FONT>
USER><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">dc.test</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">test</FONT><FONT COLOR="#000000">()</FONT>
3
yellow,red
Also look at Using Document Database (DocDB).
Another option without %ZEN.Auxiliary:
<FONT COLOR="#000080">Class dc.mylist Extends %ListOfDataTypes
</FONT><FONT COLOR="#000000">{
</FONT><FONT COLOR="#000080">Method </FONT><FONT COLOR="#000000">SizeSet(</FONT><FONT COLOR="#ff00ff">newvalue </FONT><FONT COLOR="#000080">As %Integer</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Status
</FONT><FONT COLOR="#000000">{
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#000000">i%Size=</FONT><FONT COLOR="#800000">newvalue
</FONT><FONT COLOR="#0000ff">q $$$OK
</FONT><FONT COLOR="#000000">}
}</FONT>
<FONT COLOR="#000080">Class dc.test </FONT><FONT COLOR="#000000">[ </FONT><FONT COLOR="#000080">Abstract </FONT><FONT COLOR="#000000">]
{
</FONT><FONT COLOR="#000080">/// d ##class(dc.test).test()
ClassMethod </FONT><FONT COLOR="#000000">test()
{
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">json</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#ff00ff">[</FONT><FONT COLOR="#008000">"green"</FONT><FONT COLOR="#808080">,</FONT><FONT COLOR="#008000">"yellow,red"</FONT><FONT COLOR="#808080">,</FONT><FONT COLOR="#008000">"blue"</FONT><FONT COLOR="#ff00ff">]
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%Document.Object</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">CSON</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">json</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%ToJSON</FONT><FONT COLOR="#000000">())
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">dc.mylist</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">m </FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Data</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">"_data"
zk </FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Data
s </FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Size</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">t</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Count</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Count</FONT><FONT COLOR="#000000">(),!,</FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">GetAt</FONT><FONT COLOR="#000000">(2)
}
}</FONT>