1

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..!!

Yusuf
  • 169
  • 8

1 Answers1

1

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.

Player1st
  • 1,575
  • 10
  • 14
  • Thank you for quick response. I am able to get the values. Our version of IBM i is V7R2, Let me have a look at other options you have suggested. Thanks again. – Yusuf Apr 13 '18 at 03:21