Trouble reading in an array

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
jeff.dunasky
Posts: 2
Joined: Sun Dec 03, 2023 11:37 pm

Trouble reading in an array

Post by jeff.dunasky »

This code:
list = YAJL_object_find(docNode: 'list');

i = 0;
dow YAJL_ARRAY_LOOP( list: i: node );

lastElem = i;

val = YAJL_object_find(node: 'invoice');
result.list(i).inv = yajl_get_string(val);

val = YAJL_object_find(node: 'date');
dateUSA = yajl_get_string(val);
result.list(i).date = %dec(%date(dateUSA:*usa):*iso);

val = YAJL_object_find(node: 'name');
result.list(i).name = yajl_get_string(val);

val = YAJL_object_find(node: 'amount');
result.list(i).amount = yajl_get_number(val);

val = YAJL_object_find(node: 'weight');
result.list(i).weight = yajl_get_number(val);

enddo;

works as expect when reading this JSON data:
{
"success": true,
"errmsg": "",
"list": [
{
"invoice": "70689",
"date": "03/01/2014",
"name": "SCOTT KLEMENT",
"amount": 14.80,
"weight": 3.5
}
]
}

but this code:
list = YAJL_object_find(docNode: 'jobs');

i = 0;
dow YAJL_ARRAY_LOOP( list: i: node );

lastElem = i;

val = YAJL_object_find(node: 'invoice');
result.list(i).inv = yajl_get_string(val);

val = YAJL_object_find(node: 'date');
dateUSA = yajl_get_string(val);
result.list(i).date = %dec(%date(dateUSA:*usa):*iso);

val = YAJL_object_find(node: 'name');
result.list(i).name = yajl_get_string(val);

val = YAJL_object_find(node: 'amount');
result.list(i).amount = yajl_get_number(val);

val = YAJL_object_find(node: 'weight');
result.list(i).weight = yajl_get_number(val);

enddo;

Returns a null value for list, when reading this JSON data:
{
"meta": {
"next_page": "https://discovery.bloomreach.com/dataco ... dex_update,"
},
"data": {
"jobs": [
{
"id": "fca01709-a165-4b0f-927c-07ea54724738",
"stats": {
"invalid_terms_truncated_to_single_value_count": 7,
"product_doc_count": 2002955,
"search_product_count": 5989,
"variant_doc_count": 0
},
"properties": {
"config_etag": "6f60c17ed8ee05e98a7549bdecf18c92",
"index_config_etag": "538d0e39742de2cd22c3119466054219",
"index_mode": "update",
"runnum": "20250422-185842"
},
"type": "index",
"status": "success",
"status_code": 200,
"started_at": "2025-04-22T19:02:30.053600Z",
"created_at": "2025-04-22T19:02:25.616440Z",
"updated_at": "2025-04-22T19:12:06.752881Z",
"stopped_at": "2025-04-22T19:12:06.752881Z"
}
]
}
}
jonboy49
Posts: 244
Joined: Wed Jul 28, 2021 8:18 pm

Re: Trouble reading in an array

Post by jonboy49 »

Been a long time since I used the "raw" YAJL APIs. I tend to use DATA-INTO with YAJLINTO for most stuff.

That said - I seem to recall that YAJL_object_find will only locate children of the passed node. So you would need to first locate the "data" node and then from there locate the child "jobs".

In other words:

Code: Select all

dataNode = YAJL_object_find(docNode: 'data');
list = YAJL_object_find(dataNode: 'list');
Unless you have specific needs for direct yAJL usage DATA-INTO will significantly simplify your code.
Post Reply