I can't parse the json with the cJson class. Do I have the group well defined?

Good night,

I am trying to obtain the values of the json that I indicate, but for some fields I do not get the result, such as: ns2:dProtConsLote or ns2:dCodRes

Where is my mistake. Do I have the group well defined?

I have the following json, which I read from a file:

{
    "success": true,
    "environment": "test",
    "xml": []
    "msg": {
        "ns2:rResEnviLoteDe": {
            "$": {
                "xmlns:ns2": "http://ekuatia.set.gov.py/sifen/xsd"
            },
            "ns2:dFecProc": "2023-12-14T07:12:48-03:00",
            "ns2:dCodRes": "0300",
            "ns2:dMsgRes": "Lote recibido con éxito",
            "ns2:dProtConsLote": "11453601354823256",
            "ns2:dTpoProces": "0"
        },
        "id": "647695277"
    }
}

the definition of the group

GJson                   Group
success                     STRING(5)
environment                 STRING(255),Name('environment')
qxml                        &QUEUE,Name('xml')
G2                          Group,Name('msg')
G3                              Group,Name('ns2:rResEnviLoteDe')
G4                                  Group,Name('$')
xmlns:ns2                               STRING(255),Name('xmlns:ns2')
                                    End
ns2:dFecProc                        STRING(255),Name('ns2:dFecProc')
ns2:dCodRes                         STRING(255),Name('ns2:dCodRes')
ns2:dMsgRes                         STRING(255),Name('ns2:dMsgRes')
ns2:dProtConsLote                   STRING(255),Name('ns2:dProtConsLote')
ns2:dTpoProces                      STRING(255),Name('ns2:dTpoProces')
                                End
id                              STRING(255),Name('id')
                            End
                        End

qxml                    QUEUE
Str                         STRING(20000)
                        END

my code

        root &= jsonFactory.ParseFile('respuesta.json')
        IF root &= NULL
            !error
            MESSAGE('Syntax error near: '& jsonFactory.GetError() &'|at position '& jsonFactory.GetErrorPosition())
            RETURN
        END
  
        
        
        !see the resulting json
        json::DebugInfo(root.ToString(TRUE)) !formatted outpur
        !MESSAGE(root.ToString(TRUE)) !formatted outpur
            
        GJson.qxml &= qxml
        
        IF NOT jsonFactory.ToGroup(root.ToString(), GJson, FALSE, '[{{"name":"xml", "instance":'& INSTANCE(qxml, THREAD()) &'}]')
            MESSAGE('error: ' & jsonFactory.GetError())
            RETURN
        END      
        
        MESSAGE('GJson.success: ' & CLIP(LEFT(GJson.success)) | 
            & '|id: ' & CLIP(LEFT(GJson.G2.id)) | 
            & '|environment: ' & CLIP(LEFT(GJson.environment)) | 
            & '|ns2:dCodRes: ' & CLIP(LEFT(GJson.G2.G3.ns2:dCodRes)) | 
            & '|ns2:dProtConsLote: ' & CLIP(LEFT(GJson.G2.G3.ns2:dProtConsLote)) |
            & '|xmlns:ns2: ' & CLIP(LEFT(GJson.G2.G3.G4.xmlns:ns2)) |     
            & '|records xml: ' & CLIP(LEFT(records(qxml))) |     
            )      
       

        
        !dispose all cJSON objects at once
        root.Delete()

imagen

thank you very much for your help

I use capesofts jcode website generator
https://www.capesoft.com/jFilesCode
Although it does appear to have trouble with what you have there

1 Like

Hmm Needed a , on the end of the xml line, then it comes out with this:

element                  Group,Name('element')
success                    Byte,Name('success | boolean')
environment                STRING(255),Name('environment')
xml                        String(240) ,Dim(10),Name('xml')
msg                        Group,Name('msg')
ns2:rResEnviLoteDe           Group,Name('ns2:rResEnviLoteDe')
                               Group,Name('$')
xmlns:ns2                        STRING(255),Name('xmlns:ns2')
                               End
ns2:dFecProc                   STRING(255),Name('ns2:dFecProc')
ns2:dCodRes                    STRING(255),Name('ns2:dCodRes')
ns2:dMsgRes                    STRING(255),Name('ns2:dMsgRes')
ns2:dProtConsLote              STRING(255),Name('ns2:dProtConsLote')
ns2:dTpoProces                 STRING(255),Name('ns2:dTpoProces')
                             End
id                           STRING(255),Name('id')
                           End
                         End

json                     jsonClass

  CODE
  
  !-- Load From StringTheory object into Structure---
  json.start()
  json.SetTagCase(jf:CaseAsIs)
  json.Load(element,SomeLoadString) ! Load From a StringTheory object
1 Like

Colons in field names is the problem. You can define the matches for names in the options like this:

sRules                        STRING(2048), AUTO

    sRules =  '' |
      & '[{{"name":"xml", "instance":'& INSTANCE(qxml, THREAD()) &  '},'  |
      & '{{"name":"rResEnviLoteDe","jsonname":"ns2:rResEnviLoteDe"},'     |
      & '{{"name":"ns2","jsonname":"xmlns:ns2"},'                         |
      & '{{"name":"dFecProc","jsonname":"ns2:dFecProc"},'                 |
      & '{{"name":"dCodRes","jsonname":"ns2:dCodRes"},'                   |
      & '{{"name":"dMsgRes","jsonname":"ns2:dMsgRes"},'                   |
      & '{{"name":"dProtConsLote","jsonname":"ns2:dProtConsLote"},'       |
      & '{{"name":"dTpoProces","jsonname":"ns2:dTpoProces"}]'
    jRoot.ToGroup(GJson, FALSE, sRules)

image

2 Likes

Oh! very interesting!! Thank you so much !!