I can't get the json done correctly (jSON Class)

Hello everyone !!!

I need to generate the following json:

[{
        "id": "A001",
        "codigoAlternativo": "A001",
        "unidadMedida": "Unidad",
        "descripcion": "Galletita",
        "idDepartamento": 1,
        "atributos": ["Envase" , "Pesable" ],
        "precio": 12.23,
        "idIVA": 1,
        "idIVADiferencial": 0,
        "impuestoInterno": 0,
        "valorUsuario1": "",
        "valorUsuario2": "",
        "cantidadPresentacion": 1,
        "unidadesPresentacion": "Unidad",
        "frentes": 1
    }
]

The problem I have is in the “atributos” array that generates it like this:

 "atributos": [{
                "NAME": "Envase"
            }, {
                "NAME": "Pesable"
            }
        ]

the complete json:

[{
        "id": "A001",
        "codigoAlternativo": "A001",
        "unidadMedida": "Unidad",
        "descripcion": "Alfajor",
        "idDepartamento": 1,
        "atributos": [{
                "NAME": "Envase"
            }, {
                "NAME": "Pesable"
            }
        ],
        "precio": 12.23,
        "idIVA": 1,
        "idIVADiferencial": 0,
        "impuestoInterno": 0,
        "valorUsuario1": "",
        "valorUsuario2": "",
        "cantidadPresentacion": 1,
        "unidadesPresentacion": "Unidad",
        "frentes": 1
    }
]

How is it resolved?

thank you so much

Below my code:

DATA

QatributosType      QUEUE, TYPE
name                    STRING(50)
                    END

Qplu                QUEUE
id                      STRING(30),NAME('id')
codigoAlternativo       STRING(30),NAME('codigoAlternativo')
unidadMedida            STRING(30),NAME('unidadMedida')
descripcion             STRING(30),NAME('descripcion')
idDepartamento          LONG,NAME('idDepartamento')
atributosQueue          &QatributosType,NAME('atributos')
atributosQueueInstance  LONG
precio                  REAL,NAME('precio')
idIVA                   LONG,NAME('idIVA')
idIVADiferencial        LONG,NAME('idIVADiferencial')
impuestoInterno         REAL,NAME('impuestoInterno')
valorUsuario1           STRING(30),NAME('valorUsuario1')
valorUsuario2           STRING(30),NAME('valorUsuario2')
cantidadPresentacion    REAL,NAME('cantidadPresentacion')
unidadesPresentacion    STRING(30),NAME('unidadesPresentacion')
frentes                 LONG,NAME('frentes')
                    END

CODE

    CLEAR(Qplu)    
    Qplu.id = 'A001'
    Qplu.codigoAlternativo    = 'A001'
    Qplu.unidadMedida         = 'Unidad'
    Qplu.descripcion          = 'Alfajor'
    Qplu.idDepartamento       = 1
    Qplu.precio               = 12.23
    Qplu.idIVA                = 1
    Qplu.idIVADiferencial     = 0
    Qplu.impuestoInterno      = 0
    Qplu.valorUsuario1        = ''
    Qplu.valorUsuario2        = ''
    Qplu.cantidadPresentacion = 1
    Qplu.unidadesPresentacion = 'Unidad'
    Qplu.frentes              = 1
            
            
    !- add atributos
    Qplu.atributosQueue &= NEW QatributosType
    Qplu.atributosQueueInstance = INSTANCE(Qplu.atributosQueue,THREAD())        
    CLEAR(Qplu.atributosQueue)        
    Qplu.atributosQueue.name = 'Envase'
    ADD(Qplu.atributosQueue)
    CLEAR(Qplu.atributosQueue)    
    Qplu.atributosQueue.name = 'Pesable'
    ADD(Qplu.atributosQueue)
            
    ADD(Qplu)
        
    root &= json::CreateArray(Qplu,FALSE,'[{{"name":"atributos", "isQueue":true}]')
      
    json::DebugInfo(root.ToString(TRUE))
    SETCLIPBOARD(root.ToString(TRUE))   

Sorry, I copied the json wrong

In array I have to generate it this way:

"atributos": "["Envase","Pesable"]"

They told me I forgot to put double quotes before [ and after ]

This is invalid json. “atributos”: [“Envase” , “Pesable” ] is valid. You can make it changing json::CreateArray call to this one:

root &= json::CreateArray(Qplu,FALSE,'[{{"name":"atributos", "isQueue":true, "FieldNumber": 1}]')

2 Likes

No, that’s not good json. You just want the brackets, with the quoted stuff inside. No quotes outside.

I apologize, here I leave the correct json that I took directly from the server.

{
  "id": "H0008",
  "codigoAlternativo": "2",
  "unidadMedida": "Unidad",
  "descripcion": "FIAMBRERIA",
  "idDepartamento": 1,
  "atributos": [
    "Envase",
    "Fraccionable",
    "Pesable"
  ],
  "precio": 15,
  "idIVA": 5,
  "idIVADiferencial": 0,
  "impuestoInterno": 23,
  "valorUsuario1": "",
  "valorUsuario2": "",
  "cantidadPresentacion": 1,
  "unidadesPresentacion": "Unidad",
  "frentes": 1
}

Thank you very much to both.

With Mike’s guidance it worked great.

1 Like