Fixing SQLRPGLE JSON_OBJECT SQLCODE -332 (SQLSTATE 57017) on IBM i 7.3

-

Fixing SQLRPGLE JSON_OBJECT SQLCODE -332 (SQLSTATE 57017) on IBM i 7.3

The Problem

You’re writing a SQLRPGLE program on IBM i 7.3 and trying to build JSON with JSON_OBJECT. Even the simplest example fails:

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-s PAYLOAD varchar(1000);

EXEC SQL
    SELECT CAST(
               JSON_OBJECT(
                  'Test' VALUE 'hello'
               ) AS CLOB(1000000))
      INTO :PAYLOAD
      FROM SYSIBM.SYSDUMMY1;

*inlr = *on;
return;

Instead of valid JSON, you get:

SQLCODE: -332
SQLSTATE: 57017
Message: Character conversion not defined

Even declaring PAYLOAD as an SQL CLOB with CCSID 1208 doesn’t help:

dcl-s PAYLOAD SQLTYPE(CLOB:1000000) CCSID(1208) INZ;

Why This Happens

The root issue is CCSID mismatch.

  • Your job CCSID = 1141 (EBCDIC with Euro).
  • JSON_OBJECT in IBM Db2 for i always returns data in UTF-8 (CCSID 1208).
  • When Db2 tries to return the JSON string into your RPG variable with CCSID 1141, conversion fails because no mapping exists for certain UTF-8 characters to 1141.

This is why SQLCODE -332 fires: “Character conversion is not defined between the source CCSID and the target CCSID.”


How to Fix It

Option 1 — Declare the host variable as CLOB CCSID 1208

Instead of varchar(1000) in job CCSID, define your RPG variable to explicitly accept UTF-8:

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-s PAYLOAD SQLTYPE(CLOB:1000000) CCSID(1208);

EXEC SQL
    SELECT JSON_OBJECT('Test' VALUE 'hello')
      INTO :PAYLOAD
      FROM SYSIBM.SYSDUMMY1;

*inlr = *on;
return;

This tells Db2 that the host variable expects UTF-8, matching the JSON function’s return.


Option 2 — Wrap the JSON in CAST(... AS VARCHAR CCSID 1208)

If you want a simple character field instead of a CLOB, force the conversion inside SQL before it hits RPG:

dcl-s PAYLOAD varchar(1000) ccsid(1208);

EXEC SQL
    SELECT CAST(
              JSON_OBJECT('Test' VALUE 'hello')
              AS VARCHAR(1000) CCSID 1208
           )
      INTO :PAYLOAD
      FROM SYSIBM.SYSDUMMY1;

Option 3 — Change job or program CCSID (not recommended)

You could run the job in CCSID 1208 or change your SQL environment to use UTF-8. But this affects the entire job and may break legacy logic. It’s better to just mark the JSON fields as UTF-8.


Best Practices for JSON in SQLRPGLE

  • Always use UTF-8 CCSID (1208) for JSON host variables.
  • Use CLOB(1M) CCSID 1208 or VARCHAR CCSID 1208 depending on expected size.
  • If you need to consume JSON in EBCDIC-only contexts, store it as UTF-8 and convert at the edge of your system.
  • On IBM i 7.3 and later, JSON functions (JSON_OBJECT, JSON_ARRAY, etc.) are fully supported but assume modern UTF-8 encoding.

Conclusion

The SQLCODE -332 (SQLSTATE 57017) error when using JSON_OBJECT in SQLRPGLE isn’t a bug in Db2—it’s a CCSID mismatch. JSON is UTF-8 by default, and RPG jobs running with EBCDIC CCSIDs (like 1141) can’t automatically convert. The solution is to declare your host variables as UTF-8 (CCSID 1208) or cast the SQL result to UTF-8 before returning. Once you do, JSON_OBJECT works perfectly in SQLRPGLE programs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent comments