Json document with nested arrays, one without name/id

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
panders400
Posts: 7
Joined: Thu Jul 20, 2023 8:11 pm

Json document with nested arrays, one without name/id

Post by panders400 »

I am attempting to create the DS for API. YajlGen got me most of the way there, however one nested array it couldn't/wouldn't create. I have nested arrays, but only the outer one has a name. How can you define this DS and hopefully have it created with Data-Gen using YAJL? Or do I have to edit the returned result to add the additional [ ]?

Code: Select all

 "vpoShipDtlDtoGroupList": [
        [
            {
                "qty": 24,
                "vpoItemRef": "641-1",
                "vpoItemId": {
                    "itemId": {
                        "refNo": "ITM2308-000026"
                    },
                    "lotNo": "1"
                },
                "vpoShipRef": "shipment-1",
                "shipmentNo": "shipment-1",
                "customFields": {
                    "customFieldDecimal1": 37.6896
                },
                "remarks": "Lin Item Notes / Instructions\nline break1\nline break2\n\n\nline break3\nline break4"
            }],
            [{
                "qty": 24,
                "vpoItemRef": "681-1",
                "vpoItemId": {
                    "itemId": {
                        "refNo": "ITM2308-000028"
                    },
                    "lotNo": "1"
                },
                "vpoShipRef": "shipment-2",
                "shipmentNo": "shipment-2",
                "customFields": {
                    "customFieldDecimal1": 37.6896
                }, 
                "remarks": "Lin Item Notes / Instructions\nline break1\nline break2\n\n\nline break3\nline break4"
            }],
            [{
                "qty": 24,
                "vpoItemRef": "241-1",
                "vpoItemId": {
                    "itemId": {
                        "refNo": "ITM2308-000027"
                    },
                    "lotNo": "1"
                },
                "vpoShipRef": "shipment-3",
                "shipmentNo": "shipment-3",
                "customFields": {
                    "customFieldDecimal1": 51.2496
                }, 
                "remarks": "Lin Item Notes / Instructions\nline break1\nline break2\n\n\nline break3\nline break4"
            }]
    ],
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Json document with nested arrays, one without name/id

Post by Scott Klement »

Is this just an excerpt from a larger JSON document? As it sits, it's not a valid JSON document. It starts with a property name, and property names only exist within objects, they can't be given at the very start of the document.

I don't see anything "without a name", but "vpoShipDtlDtoGroupList" is a two-dimenational array. RPG doesn't support two dimensional arrays, so you'll have to use the YAJL subprocedures instead of DATA-INTO, and map the data into a different format since there isn't a one-to-one correlation between this data and an RPG variable (owing to the fact that RPG does not have two dimensional arrays.)
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: Json document with nested arrays, one without name/id

Post by jonboy49 »

I wonder Scott if this isn't worthy of an IDEA for DATA-INTO? Or is it something that possibly could be added to YAJLINTO to supply a "made-up" name to RPG when encountering such an element?

RPG does support effectively support multi-dimensional arrays but the inner elements need a group name which JSON doesn't require.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Json document with nested arrays, one without name/id

Post by Scott Klement »

jonboy49 wrote: Mon Aug 14, 2023 4:10 pm I wonder Scott if this isn't worthy of an IDEA for DATA-INTO? Or is it something that possibly could be added to YAJLINTO to supply a "made-up" name to RPG when encountering such an element?
I don't see what I'd request from IBM, here? I suppose it'd be "two dimensional arrays"...?
RPG does support effectively support multi-dimensional arrays but the inner elements need a group name which JSON doesn't require.
While nested elements can serve the same purpose as a multi-dimentional array, they aren't exactly the same thing, syntactically. In JSON data can be sent as a nested array like this:

Code: Select all

{
  "mydata": [{
     "innerdata": [  x, y, z ]
  }]
}
But it could also be sent like this:

Code: Select all

{
  "mydata": [[ x, y, z ]]
}
So both possibilities exist, and they aren't exactly the same, even if they can be used for the same purpose.

YAJLINTO could have a "feature" added to tranlate from the two-dimentional array to the nested element array... certainly possible to do that without asking IBM to implement an "idea" -- could be done entirely within YAJLINTO.

But... how would you do it in the opposite direction? DATA-GEN builds the JSON based on the format of the RPG variable... though, I guess you could designate an explicit name for the nested value, sort of like XML-INTO's "datasubf", but I guess it'd be "multiarraysubf" or something...

All of these kludgy options do make it harder for people trying to learn to process XML/JSON in RPG, though...
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: Json document with nested arrays, one without name/id

Post by jonboy49 »

Scott, I was thinking last night that there was not really anything that DATA-INTO could do - so I agree an IDEA is not an option.

Agreed that changing YAJLINTO would also add complexity to the learning process.

JSON is "better" than XML in part due to its flexibility and simpler rules - but it is a royal pain sometimes.
panders400
Posts: 7
Joined: Thu Jul 20, 2023 8:11 pm

Re: Json document with nested arrays, one without name/id

Post by panders400 »

Thank you for your analysis. I was able to utilize JonBoy's idea of naming it for the yajldtagen and then modifying the outbound JSON document. I believe I am having basically the same problem with the returned response, even though I only need a small portion of it. At least I know how to handle the situation in my programs.

I appreciate the assistance.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Json document with nested arrays, one without name/id

Post by Scott Klement »

I guess I don't see this as a problem with JSON.

Basically any language out there that people do today's coding in... C, C++, Basic, C#, Objective C, Java, PHP, Python, JavaScript/Node, Ruby, etc,... they all have multi-dimension arrays. What languages don't? RPG... Cobol, maybe?, obviously a few oddball things like CL that are used in very specialized environments... but to me it makes perfect sense that a data format intended to serialize data from programming languages would support what just about every programming language on earth supports.

The problem is that RPG doesn't support multi-dimension arrays, and wants to play in the same arena as other languages.
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: Json document with nested arrays, one without name/id

Post by jonboy49 »

Scott Klement wrote: Wed Aug 16, 2023 8:21 pm <snip>

The problem is that RPG doesn't support multi-dimension arrays, and wants to play in the same arena as other languages.
So you're saying that we need something like Dim( 20 : 10 ) or similar?

I confess that when working with languages that allow it, I have always found it a little frustrating to not have a name associated with the inner level. But that's just me - having spent most of my working life with languages like RPG and COBOL that require that (effectively) nested arrays have a name.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Json document with nested arrays, one without name/id

Post by Scott Klement »

jonboy49 wrote: Wed Aug 16, 2023 9:15 pm So you're saying that we need something like Dim( 20 : 10 ) or similar?

I confess that when working with languages that allow it, I have always found it a little frustrating to not have a name associated with the inner level. But that's just me - having spent most of my working life with languages like RPG and COBOL that require that (effectively) nested arrays have a name.
Yes, something like that.

I agree that it's nicer to have an inner name -- and all modern languages allow for that, so that's already covered. The problem is that languages need compatible features to fully interoperate with each other. If RPG is the only modern programming language that doesn't have multi-dim arrays, it can't fully operate.

You can "they shouldn't use them in the other languages either!" if you want to -- but the reality is, they will do so regardless. And we won't be able to interoperate.
jonboy49
Posts: 206
Joined: Wed Jul 28, 2021 8:18 pm

Re: Json document with nested arrays, one without name/id

Post by jonboy49 »

No argument.

I'll have a think about whether it is worth floating an IDEA to have it added to RPG. No way you'd ever get it in COBOL.
Post Reply