JSON simple problem

I need to create a simple JSON string, didn’t deal with JSON yet but i’m familiar with xml.
In XML it’s easy with XML:CreateParent and XML:CloseParent, but how to do next example in JSON? I have jFiles but not sure do i even need it for that simple string. In manuals there are lots of examples with files but i just need manually format string, think i’m sick today and my brain is foggy…

{
    "operation":"x",
    "amount":"10"
    "config":{		!!! stuck
       "header":"xy",
       "options":[
          a,
          b,
          c
       ],
       "extra":true
    }
}

so i start:

   myJSON.start()
   myJSON.add('operation','x')
   myJSON.add('amount','10')

and stuck…

or should i just write: string='{"operation":"x",""amount":"10"... ?

help, thanks!

Greetings-

If you use xFiles and jFiles this should be fairly easy.
If you do not, it might take a bit longer.

Capesoft has a website that will take XML or JSON and convert it to Clarion code and will do the reverse operation.

If you have a Clarion structure (File, group, etc.) and you are already producing XML with it, the same structure will let you output into JSON.

ClarionLive webinar 707 “Fun with XML and JSON” https://www.youtube.com/live/mwXsowDRixo

Hope that helps.

Regards,
Roberto Artigas

1 Like

As Roberto pointed out the easiest way is to create a GROUP in clarion that matches your target json. Capesoft provides a website to help with that. https://capesoft.com/jfilescode

There you can paste your json example and it will generate the group and code examples for either creating a json string based on the example or how to read a json string based on the example into your Clarion code.
After fixing a couple of issues in your posted json. I came up with this:

{
    "operation": "x",
    "amount": "10",
    "config": {
        "header": "xy",
        "options": [
            "a",
            "b",
            "c"
        ],
        "extra": true
    }
}

The jfilescode created this example.

element                  Group,Name('element')
operation                  STRING(255),Name('operation')
amount                     STRING(255),Name('amount')
config                     Group,Name('config')
header_                      STRING(255),Name('header')
options                      STRING(255),Dim(10),Name('options')
extra                        Byte,Name('extra | boolean')
                           End
                         End

json                     jsonClass
SomeSaveString StringTheory
  CODE
  
  !-- Save From Structure into StringTheory object ---
  json.start()
  json.SetTagCase(jf:CaseAsIs)
  json.SetDontSaveBlankArrayValues(true)
  element.Operation = 'x'
  element.amount = 10
  element.config.header_ = 'xy'
  element.config.options[1] = 'a'
  element.config.options[2] = 'b'
  element.config.options[3] = 'a'
  element.config.extra = true
  json.Save(element,SomeSaveString) ! Save to a StringTheory object

! Now SomeSaveString holds your json string

You can build it up manually, but it is more work.
Something along these lines. This is untested code.

json JSONClass
jConfig &JSONClass
jOptions &JSONClass
options     string(5),dim(10)
SomeSaveString StringTheory
  CODE
  json.Start()
  json.Add('operation', 'x')
  json.Add('amout', '10')
  jConfig &= json.CreateCollection('config', true)
  jConfig.Add('header','xy')
  options[1] = 'a'
  options[2] = 'b'
  options[3] = 'c'
  jOptions &= jConfig.AddArray('options',options)
  jConfig.Add('extra',true, json:Boolean)
  json.SaveString(SomeSaveString)
  SomeSaveString.Trace()

The key for “sub” objects are the CreateCollection and AddArray methods.
This should get you close.

2 Likes