Search code examples
rpgleyajl

How to retrieve JSON key having array of values using YAJL RPGLE


I am just starting to explore on writing/parsing JSON using YAJL in RPGLE

I am encountering a situation were i need to parse a key with array of values

e.g. {"key":[value1,value2,value2]}

I am seeing examples to parse array of objects i.e. { "key" : [ {"k1":"v1"}, {"k2":"v2"} , {"k3":"v3"} ] }

list = YAJL_object_find(docNode: 'key');
i = 0;
dow YAJL_ARRAY_LOOP( list: i: node );
  val = YAJL_object_find(node: 'k1');
  value1 = yajl_get_string(val);
enddo;

But not for the array of values for single Key. Any idea how we can do this using YAJL in RPGLE.

Thanks in Advance..!!


Solution

  • This is simply a matter of removing a line in your example and making a small modification. You have no need to look for the object in the array loop because you already have the relevant value.

    list = YAJL_object_find(docNode: 'key');
    i = 0;
    dow YAJL_ARRAY_LOOP( list: i: node );
      value1 = yajl_get_string(node);
    enddo;
    

    If you are on the latest version of IBM i (7.3 TR4 as of this comment), you should probably look into using the DATA-INTO RPG opcode or if you want it in a relational format, you can use SQL JSON_TABLE.