Load a JSON string into a data structure.
Also, create a new data structure and serialize it into JSON.

Use objects and arrays (as appropriate for your language)
and make sure your JSON is valid (https://jsonformatter.org).

 
MODULE A {
      \\ Process data in json format
 
      \\ We can load from external file with Inline "libName"
      \\ or multiple files Inline "file1" && "file2"
      \\ but here we have the library in a module
      Inline Code Lib1
      \\ So now we make a Parser object (a group type in M2000)
      Parser=ParserClass()
      \\ We can display any function, module that is public and known list
      Modules ?
      \\ And this are all known variables (or and objects)
      List !
      Document json$
      \\ We can load from file
      \\ Load.Doc json$, "alfa.json"
      json$={{
            "alfa":-0.11221e+12,
            "array" : [
                  -0.67,
                  "alfa1",
                  [
                        10,
                        20
                  ],
                  "beta1",
                  1.21e12,
                  21.12145,
                  "ok"
            ],
            "delta": false, "epsilon" : true, "Null Value" : null
      }}
      Save.Doc json$, "json2.json"    \\ by default in Utf-8 with BOM
      \\ just show multiline text
      \\ Report display lines and stop after 3/4 of console height lines
      \\ just press a key or click mouse button
      Report json$
      \\ so now we get text to a new object
      alfa=Parser.Eval(json$)
      \\ check it
      Print Type$(alfa) ' it is a group
      Print "alfa.type$=";alfa.type$ \\ this is a read only property
 
      Report "as one line"
      Report Parser.Ser$(alfa, 0)
 
      Report "as multiline"
      Report Parser.Ser$(alfa, 1)
 
      Print "Using Print"
      Print Parser.ReadAnyString$(alfa)
 
      Print "Value for alfa, id alfa"
      Print Parser.ReadAnyString$(alfa,"alfa")
      Report "as multiline"
      Report Parser.Ser$(Parser.Eval(Parser.ReadAnyString$(alfa,"array", 2)), 1)
      \\ We get a copy of an array as a Group (a group which return an array)
      Alfa3=Parser.Eval(Parser.ReadAnyString$(alfa,"array", 2))
      \\ First value is for actual object, second value is a readonly property of this object
      Print type$(Alfa3), Alfa3.type$
      Dim B()
      \\ Now Alfa3 run Value part and pass a pointer of array
      \\  B() is an array and here take a pointer to Alfa3 array (as value of Alfa3)
      B()=Alfa3
      \\ each() make an iterator for B()
      N=each(B())
      While N {
            \\ Using B() we get values always. but if we have "object" or "array" then Print prints items **
            Print B(N^)
      }
      \\ Print show here nothing because if value is object then "print" just leave a column and continue to next one
      Print B()
      \\ we have to use Group() to get group not value of group (if any).
      \\ Group() works for "named" group, not for stored in an array or an inventory or a stack
      Print Parser.StringValue$(Group(Alfa3), 0)
      Print Parser.StringValue$(Group(Alfa3), 1)
      \\ Now we want to pass a new value
      \\ Interpreter want to match type of expression from left side to right side
      \\ Because Parser.StringValue$ is actual a Group (As property),
      \\ we have a second linked name:  Parser.StringValue
      \\ we have to use Parser.StringValue()
      \\ and all values must be groups, as those provided by Parser
      Parser.StringValue(Group(Alfa3), 1)=Parser.Numeric(1234)
      Print Parser.StringValue$(Group(Alfa3), 1)
      Print Parser.StringValue$(Group(Alfa), "array", 2, 0)
      \\ we have to use Parser.StringValue$()
      Parser.StringValue$(Group(Alfa), "array", 2, 0)=Parser.JString$("Changed to String")
      Print Parser.StringValue$(Group(Alfa), "array", 2,0)
      Try ok {
            Print Parser.StringValue$(Group(Alfa), "array", 2)
      }
      If Error or not ok Then Print Error$
      Parser.StringValue.Add = True
      Parser.StringValue$(Group(Alfa), "array", 2, 10)=Parser.JString$("Changed to String 2")
      Parser.StringValue(Group(Alfa), "Last value")=Parser.Boolean(true)
      Report "as multiline"
      Report Parser.Ser$(alfa3, 1)
      Report Parser.Ser$(alfa, 1)
      Parser.StringValue.Add = False
      Parser.StringValue.Del = True
      Parser.StringValue(Group(Alfa), "array", 0)=Parser.Null()
      Parser.StringValue(Group(Alfa), "delta")=Parser.Null()
      Parser.StringValue.Del = False
      For Parser {
            .StringValue(Group(Alfa), "array", 1,5)=.Arr((.Numeric(10), .Jstring$("ok 20"), .Boolean(true)))
      }
      Report Parser.Ser$(alfa, 1)
 
}
// call A
A
 