Article
· Jan 24, 2022 2m read

GlobalToJSON-Efficient

I have created a package that offers a utility to load a Global into JSON object and reverse     
to create a Global from this type of JSON object. Efficient refers to the structure created.
Only Globals nodes containing data are presented in the generated JSON object.

The export of this Global 

Generates a JSON object that covers all Global nodes 

USER>write $$Do^ZPretty(json)
{
  "gbl":[
    {
      "node":"^dc.MultiD",
      "val":"5"
    },
    {
      "node":"^dc.MultiD(1)",
      "val":"$lb(\"Braam,Ted Q.\",51353)"
    },
    {
      "node":"^dc.MultiD(1,\"mJSON\")",
      "val":"{}"
    },
    {
      "node":"^dc.MultiD(2)",
      "val":"$lb(\"Klingman,Uma C.\",62459)"
    },
    {
      "node":"^dc.MultiD(2,2,\"Multi\",\"a\")",
      "val":"1"
    },
    {
      "node":"^dc.MultiD(2,2,\"Multi\",\"rob\",1)",
      "val":"rcc"
    },
    {
      "node":"^dc.MultiD(2,2,\"Multi\",\"rob\",2)",
      "val":"2222"
    },
    {
      "node":"^dc.MultiD(2,\"Multi\",\"a\")",
      "val":"1"
    },
    {
      "node":"^dc.MultiD(2,\"Multi\",\"rob\",1)",
      "val":"rcc"
    },
    {
      "node":"^dc.MultiD(2,\"Multi\",\"rob\",2)",
      "val":"2222"
    },
    {
      "node":"^dc.MultiD(2,\"mJSON\")",
      "val":"{\"A\":\"ahahah\",\"Rob\":\"VIP\",\"Rob2\":1111,\"Rob3\":true}"
    },
    {
      "node":"^dc.MultiD(3)",
      "val":"$lb(\"Goldman,Kenny H.\",45831)"
    },
    {
      "node":"^dc.MultiD(3,\"mJSON\")",
      "val":"{}"
    },
    {
      "node":"^dc.MultiD(4)",
      "val":"$lb(\"\",\"\")"
    },
    {
      "node":"^dc.MultiD(4,\"mJSON\")",
      "val":"{\"rcc\":122}"
    },
    {
      "node":"^dc.MultiD(5)",
      "val":"$lb(\"\",\"\")"
    },
    {
      "node":"^dc.MultiD(5,\"mJSON\")",
      "val":"{}"
    }
  ]
}
USER>


And the related Loader creates exactly the same Global

Online Demo Terminal
Online Demo SMP

Video

GitHub

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

I have added a new version  for the GLOBALS contest
To take care of large Globals that may break your available memory.
The JSON Object is exported to a file. and there is also the related loader

New Version 0.1.0

USER>write ##class(dc.GblToJSON.EX).export("^dc.MultiD")
File gbl.json created

And the related loader creates the Global

USER>write ##class(dc.GblToJSON.EX).import()
Global ^dc.MultiD loaded

It is of course also available in the Online Demo facility.  
And to see the generated file there is a show() method

USER>write ##class(dc.GblToJSON.EX).show()
{"gbl":[
{"node":"^dc.MultiD","val":5},
{"node":"^dc.MultiD(1)","val":"$lb(\"Braam,Ted Q.\",51353)"},
{"node":"^dc.MultiD(1,\"mJSON\")","val":"{}"},
{"node":"^dc.MultiD(2)","val":"$lb(\"Klingman,Uma C.\",62459)"},
{"node":"^dc.MultiD(2,2,\"Multi\",\"a\")","val":1},
{"node":"^dc.MultiD(2,2,\"Multi\",\"rob\",1)","val":"rcc"},
{"node":"^dc.MultiD(2,2,\"Multi\",\"rob\",2)","val":2222},
{"node":"^dc.MultiD(2,\"Multi\",\"a\")","val":1},
{"node":"^dc.MultiD(2,\"Multi\",\"rob\",1)","val":"rcc"},
{"node":"^dc.MultiD(2,\"Multi\",\"rob\",2)","val":2222},
{"node":"^dc.MultiD(2,\"mJSON\")","val":"{\"A\":\"ahahah\",\"Rob\":\"VIP\",\"Rob2\":1111,\"Rob3\":true}"},
{"node":"^dc.MultiD(3)","val":"$lb(\"Goldman,Kenny H.\",45831)"},
{"node":"^dc.MultiD(3,\"mJSON\")","val":"{}"},
{"node":"^dc.MultiD(4)","val":"$lb(\"\",\"\")"},
{"node":"^dc.MultiD(4,\"mJSON\")","val":"{\"rcc\":122}"},
{"node":"^dc.MultiD(5)","val":"$lb(\"\",\"\")"},
{"node":"^dc.MultiD(5,\"mJSON\")","val":"{}"}
]}
***** gbl.json *****