Question
· Jun 8, 2021

How do I save the value in obj.%Size() to a variable ?

Hi all, I am new to IS Objectscript and I would appreciate some assistance regarding this. 

I am trying to find out how to count the number of elements within a dynamic abstract object and I am having some trouble using the size method.

Here is the code below:

The key value pairs are originally in JSON and I would have converted it in to an object for use.

Class JSON.Test {

ClassMethod Test()
{
    set obj = {"byr:":"1937", "iyr:":"2017", "eyr:":"2020", "hgt:":"183cm", "hcl:":"#FFFFFD","ecl:":"grey", "pid:":"028048884"}
    set passport1 = {"ecl:":"grey","pid:":"860033327", "eyr:":"2020","hcl:":"#FFFFFD","byr:":"1937", "iyr:":"2017", "cid:":"147", "hgt:":"183cm" }
    do ..FirstPassport(passport1)
}

ClassMethod FirstPassport(passport1 As %DynamicObject, property = "")
{
    write !
    set iterator = passport1.%GetIterator()

    write "Number of properties: "
    set property = _passport1.%Size

    // I would like to be able to save the above mentioned property as an value and use this as a condition to determine if the property is 8 then write the key value on to the screen.
    while iterator.%GetNext(.key, .value) {
        write key_value, !
    }

    write !
}

}
Product version: IRIS 2020.1
$ZV: IRIS for UNIX (Ubuntu Server LTS for x86-64 Containers) 2020.4 (Build 547U) Fri Apr 2 2021 16:44:21 EDT
Discussion (7)0
Log in or sign up to continue

Somehow I don't get you right. To save obj.%Size() in a variable, just do a simple assign

set myVariable = obj.%Size()

but I'm pretty shure, this was not your intended question.

I suppose, you have JSON formatted data (a string or a stream) and you want to store those data in a table. Am I right?

If yes, then follow the next steps:

1) create a class which describes your JSON objects (strings)

Class DC.SehindeRaji Extends (%Persistent, %JSON.Adaptor)
{
Property byr As %String(%JSONFIELDNAME = "byr:");
Property iyr As %String(%JSONFIELDNAME = "iyr:");
Property eyr As %String(%JSONFIELDNAME = "eyr:");
// do the same for all other fields

ClassMethod Import(data)
{
    set obj=..%New()                    // create a new DC.SehindeRaji object
    set sts=obj.%JSONImport(data,"")    // import the (JSON) data
    
    if sts {
        set sts = obj.%Save()
        if sts {
            write "Saved, ID=",obj.%Id(),!
            quit 1
            
        } else {
            write "Not saved, Err=",$system.Status.GetOneErrorText(sts),!
            quit 0
        }
        
    } else {
        write "Can't import: ",$system.Status.GetOneErrorText(sts),!
        quit 0
    }
}
}

2) You can create some test data (interactively) in a terminal session

set dynObj = {"byr:":"1937", "iyr:":"2017", "eyr:":"2020"}
set data = dynObj.%ToJSON()

or get your data somehow from an input (possibly from a file),  the only important thing is, your data should look like this

write data  -->  {"byr:":"1937","iyr:":"2017","eyr:":"2020"}

3) import those data

write ##class(DC.SehindeRaji).Import(data) --> Saved, ID=1

4) Now open the saved data and check the result

set oref =  ##class(DC.SehindeRaji).%OpenId(1)

write oref.byr  --> 1937
write oref.iyr  --> 2017

write oref.%JSONExportToString(.exported,"") --> 1
write exported  --> {"byr:":"1937","iyr:":"2017","eyr:":"2020"}

zw ^DC.SehindeRajiD
^DC.SehindeRajiD=1
^DC.SehindeRajiD(1)=$lb("","1937","2017","2020")

I hope, this is what yoy want to do...