If you are ever in a situation where you want to read in JSON from Standard Input (POST) using RPG for parsing, then this example is for you.
This solution uses:
The main function from the YAJL Toolkit used will be yajl_stdin_load_tree(). This function allows you to read JSON data in from Standard Input (from a POST) very easily, load it into YAJL and start parsing right away.
The RPG code for program YAJLSTDIN is as follows:
**FREE
ctl-opt DFTACTGRP(*NO) ACTGRP('YAJL') BNDDIR('YAJLBVS');
// Imports
/copy qrpglesrc,yajl_h
/copy qcopysrc,p.erpgsdk
// YAJL Variables
dcl-s docNode Like(yajl_val);
dcl-s node Like(yajl_val);
dcl-s val Like(yajl_val);
dcl-s yajl_error varchar(500);
// Work Variables
dcl-s firstName char(256);
dcl-s lastName char(256);
dcl-s errorText char(256);
exsr $Input;
exsr $Output;
exsr $Return;
//***************************************************************
// Read Input
//***************************************************************
BegSr $Input;
docNode = yajl_stdin_load_tree(*ON:yajl_error);
if (docNode = *NULL) or (yajl_error <> ' ');
yajl_tree_free(docNode);
errorText = 'Error reading JSON data. ' + %trim(yajl_error);
else;
val = YAJL_object_find(docNode:'firstName');
firstName = yajl_get_string(val);
val = YAJL_object_find(docNode:'lastName');
lastName = yajl_get_string(val);
endif;
yajl_tree_free(docNode);
EndSr;
//***************************************************************
// Write Output using ERPGSDK
//***************************************************************
BegSr $Output;
#startup();
#writeTemplate('stdhtmlheader.erpg');
#loadTemplate('yajlstdin.erpg');
if (errorText <> ' ');
#loadSection('error');
#replaceData('/%errorText%/':errorText);
else;
#loadSection('main');
#replaceData('/%firstName%/':firstName);
#replaceData('/%lastName%/':lastName);
endif;
#writeSection();
#cleanup();
EndSr;
//***************************************************************
// Return
//***************************************************************
BegSr $Return;
yajl_genClose();
*INLR = *ON;
return;
EndSr;
This program is split into 3 main sections or subprocedures:
In order to call this function, simply load up Postman and set up the application to make a call to https://www.bvstools.com/cgi-bin/yajlstdin as a POST sending the JSON data as follows (you can use any first name and last name you want):
{"firstName":"PuriPuri",
"lastName":"Prisoner"}
The Postman application should then return either an error, or report back the values you sent.
You sent the following data:
firstName=PuriPuri
lastName=Prisone
Below I have included an export of the Postman data that you can easily use to import into Postman and run. Simple copy the JSON below, save it as a .json file on your PC, and use the Import option in Postman. (Note, if you are still using the old browser app of Postman, download the PC version as the browser version is out of date an unsupported):
{
"info": {
"_postman_id": "9a70dfd9-a456-42b7-937d-f93c3ba5c927",
"name": "bvstools",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "yajlstdin",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"firstName\":\"PuriPuri\",\n \"lastName\":\"Prisoner\"}"
},
"url": {
"raw": "https://www.bvstools.com/cgi-bin/yajlstdin",
"protocol": "https",
"host": [
"www",
"bvstools",
"com"
],
"path": [
"cgi-bin",
"yajlstdin"
]
}
},
"response": []
}
]
}
Feel free to check out the other YAJL articles as well for more detail or building and parsing JSON data using YAJL:
Related Articles: