× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Peter,

You are confusing YAJL because you are trying to do something that isn't valid in a JSON document.

Only subfields in an object can have names -- but you are trying to assign names to array elements.  This makes it think that the object name was meant to be a string in the array rather than the name of an object, since its not possible for an array element to have a name.

Incidentally, the *same* is true in RPG. :-)   Consider this example:

dcl-ds myDS qualified;
  array packed(5:2) dim(3);
end-ds;

I now have an array with 3 elements.  But, I cannot name element 1 "Object x in array" or element 2 "Object y in array".  I have to refer to them as 1, 2, etc.  There can be a name for the *entire* array (myDS.array in this example), but I can't name each individual element a separate name.

On the other hand I *can* do this:

dcl-ds myDS qualified;
  dcl-ds object;
    object_x packed(5: 2);
    object_y packed(5: 2);
    object_Z packed(5: 2);
  end-ds;
end-ds;

But that's not an array, it's a data structure.  A data structure is equivalent to an "object" in JSON.  An array is equivalent to an array in JSON.  Each element in an object can have a name. Each element in an array does not have a name, but programs can refer to them as element 1, element 2, etc.

Anyway...  since the same is true in JSON, you cannot do:

yajl_beginObj();
  yajl_beginArray('array');
    yajl_beginObj('object_x'); // problem! array element cannot have a name.
    yajl_endObj();
  yajl_endArray();
yajl_endObj();

Since 'object_x' in the above example is not possible... it thinks you meant to insert a string and that the object is meant to be a separate array element.  That's just a quirk of how YAJL works under the covers.  Remember, the RPG stuff like yajl_beginObj() is just my wrapper above the standard yajl open source project -- this is a quirk about how how that underlying generator works.

Hope that explanation was easy to understand...  not sure if I'm explaining it right.

To fix your code, remove the element names for anything that's not a subfield of an object.

yajl_beginArray('array');
          yajl_beginObj();
             yajl_beginObj('Object A in objectx ');
               yajl_addChar('tag':'value');
             yajl_endObj();
          yajl_endObj();
          yajl_beginObj();
               yajl_addChar('tag':'value');
          yajl_endObj();
       yajl_endArray();

Or else consider using DATA-GEN, which makes it harder to have errors like this since you have to declare a matching RPG :-)

Good luck!

On 4/14/2021 3:01 PM, Peter Colpaert wrote:
Hi,

for a new project I'm working on creating JSON data using Scott Klement's
excellent YAJL tool.

There is one issue I'm not quite capable of solving.

Consider this (pseudo-)code:

yajl_genOpen(*On);
yajl_beginObj();

yajl_beginArray('array');
yajl_beginObj('Object x in array');
yajl_beginObj('Object A in objectx ');
yajl_addChar('tag':'value');
yajl_endObj();
yajl_endObj();
yajl_beginObj('Object y in array');
yajl_addChar('tag':'value');
yajl_endObj();
yajl_endArray();

yajl_endObj();
yajl_saveBuf('/tmp/itext1/testNesting.json': errMsg);
if errMsg <> '';
// handle error
endif;
yajl_genClose();

I would expect to receive an array of objects, where the first object
contains another object:

root object
array
object X
object A
tag : value
object Y
tag: value

However, what I get is slightly different:
root object
array
string "object x in array"
object A
tag : value
string "object Y"
namesless object
tag: value

Not sure if I'm doing something wrong or there is a but on YAJL.

Any help would be highly appreciated.

Thanks,

Peter


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.