次の例は、JSON テキスト文字列を解析して COBOL グループ項目にする方法を示したものです。
次の JSON テキスト文字列があるとします。
{"msg":{"SND":9,"pid":1289,"txt":"Hello World!"}}
この文字列を次の COBOL プログラムに渡すと、どのように解析されるかがわかり、結果を照会して操作できるようになります。
Identification division. Program-id. jparse1. Data division. Working-storage section. 01 msg. 03 snd usage comp-1. 03 pid pic 9999 usage display. 03 txt pic x(12). Linkage section. 01 json-str pic x(53). Procedure division using json-str. display "Parsing....". JSON PARSE json-str into msg with DETAIL END-JSON. if snd equal to 9 then display "PID is " pid display "Message text is '" txt "'" end-if. goback. End program jparse1.
この JSON PARSE 文を実行すると、グループ msg に JSON テキストの名前と値のペアから 3 つの値が取り込まれます。これで、任意の方法でテキストを操作できます。