How do I generate a json conditionally?

Hello everyone!!

I’m using the cJSON for Clarion parser from Mike Duglas GitHub.

I ask you about how to add an array in the “Items” conditionally.

In the example json the first item “TEST-ITM-1” has an array “external_categories”. The item “TEST-ITM-2” does not have it.

This is my code to generate the json, but I don’t realize how to conditionally add the “external_categories” array to the items.

Thank you very much.

The json to generate:

{
    "external_reference": "TEST-EXT-REF",
    "notification_url": "http://localchost",
    "title": "Orden Crear",
    "description": "Test",
    "expiration_date": "2024-07-23T00:13:36.000-04:00",
    "total_amount": 3600,
    "items": [{
            "sku_number": "779001",
            "category": "FOOD",
            "title": "TEST-ITM-1",
            "description": "TEST-ITM-1",
            "unit_price": 1000,
            "quantity": 1,
            "unit_measure": "unit",
            "total_amount": 1000,
            "external_categories": [{
                    "id": "refrescos"
                }, {
                    "id": "500ml"
                }, {
                    "id": "pomelo"
                },
            ]
        }, {
            "sku_number": "779001",
            "category": "FOOD",
            "title": "TEST-ITM-2",
            "description": "TEST-ITM-2",
            "unit_price": 2600,
            "quantity": 2,
            "unit_measure": "unit",
            "total_amount": 1300
        }
    ]
}

My code:


                    PROGRAM
    INCLUDE('cjson.inc')    

                    MAP
                        QueueTest()
                    END

    CODE
        QueueTest()
        
        
QueueTest           PROCEDURE

root                    &cJSON

itemsQType              QUEUE, TYPE
sku_number                  STRING(20),NAME('sku_number')
category                    STRING(10),NAME('category')
title                       STRING(30),NAME('title')
description                 STRING(30),NAME('description')
unit_price                  DECIMAL(15,2),NAME('unit_price')
quantity                    SHORT,NAME('quantity')
unit_measure                STRING(4),NAME('unit_measure')
total_amount                DECIMAL(15,2),NAME('total_amount')
                        END

ordencrear              GROUP
external_reference          STRING(100),NAME('external_reference')
notification_url            STRING(256),NAME('notification_url')
title                       STRING(30),NAME('title')
description                 STRING(30),NAME('description')
expiration_date             STRING(30),NAME('expiration_date')
total_amount                DECIMAL(15,2),NAME('total_amount')
itemsQueue                  &itemsQType,NAME('items')
itemsQueueInstance          LONG
                        END

    CODE
        
        CLEAR(ordencrear)
        !           Cabecera
        ordencrear.external_reference = 'TEST-EXT-REF'
        ordencrear.notification_url = 'http://localchost'
        ordencrear.title = 'Orden Crear'
        ordencrear.description = 'Test'
        ordencrear.expiration_date = FORMAT(YEAR(TODAY()),@N04)&'-'& |
            FORMAT(MONTH(TODAY()),@N02)&'-'&FORMAT(DAY(TODAY()),@N02)&'T'&FORMAT(TODAY(),@T04)&'.000-04:00'
        ordencrear.total_amount = 3600
        
        
        
        ! ITEMS
        
        ordencrear.itemsQueue &= NEW itemsQType
        ordencrear.itemsQueueInstance = INSTANCE(ordencrear.itemsQueue,THREAD())
        
        
        ordencrear.itemsQueue.category = 'FOOD'
        ordencrear.itemsQueue.description = 'TEST-ITM-1'
        ordencrear.itemsQueue.quantity = 1
        ordencrear.itemsQueue.sku_number = '779001'
        ordencrear.itemsQueue.title = 'TEST-ITM-1'
        ordencrear.itemsQueue.total_amount = 1000
        ordencrear.itemsQueue.unit_measure = 'unit'        
        ordencrear.itemsQueue.unit_price = 1000
        Add(ordencrear.itemsQueue)
        
        
        ordencrear.itemsQueue.category = 'FOOD'
        ordencrear.itemsQueue.description = 'TEST-ITM-2'
        ordencrear.itemsQueue.quantity = 2
        ordencrear.itemsQueue.sku_number = '779001'
        ordencrear.itemsQueue.title = 'TEST-ITM-2'
        ordencrear.itemsQueue.total_amount = 1300
        ordencrear.itemsQueue.unit_measure = 'unit'        
        ordencrear.itemsQueue.unit_price = 2600
        Add(ordencrear.itemsQueue)        
        
        
        root &= json::CreateObject(ordencrear,FALSE,'[{{"name":"items", "isQueue":true}]') 
        
        setclipboard(root.ToString(TRUE))
        MESSAGE(root.ToString(TRUE))
        
        DISPOSE(ordencrear.itemsQueue)
        root.Delete()

Assume you have an external categories queue (“extCatQueue”) built independantly for each item. Then you can find a json object assosiated with an items queue and add an array to it:

  !- add external_categories array
  LOOP i=1 TO RECORDS(ordencrear.itemsQueue)
    GET(ordencrear.itemsQueue, i)
    
    ReadExtCat(ordencrear.itemsQueue, extCatQueue)
    
    IF RECORDS(extCatQueue)
      jItem &= root.FindArrayItem('items', i)
      jItem.AddItemToObject('external_categories', json::CreateArray(extCatQueue))
    END
  END

Thank you very much for your great help, you have given me the solution.