Get longest strings in jsonb column I have a postgresql table with a jsonb column holding a lot of different data. This data needs to be moved into a DB2 database, but I need to know the max length of each individual "node" within the jsonb data to be sure I don't exceed the limitations of the individual DB2 column they get inserted into.

For example, {'city':'Mesa','county':'Maricopa','mayor':'John Smith'}

Is there a way to get the max length of each value in a jsonb column via SQL, resulting in something like city: 15, county: 22, mayor: 28 short of looping over every record?
If you have a flat JSONB structure, ie. no objects and arrays exist as values, then the solution is simple.  

Given the following table and data:

CREATE TABLE jsonbtest (data jsonb);

INSERT INTO jsonbtest (data) VALUES ('{"city":"Mesa","county":"Maricopa","mayor":"John Smith"}');
INSERT INTO jsonbtest (data) VALUES ('{"other city":"Mesa but with a longer name","county":"Maricopa","mayor":"John Smith Jr"}');


you can get the key-value pairs by doing

SELECT (jsonb_each_text(data)).* FROM jsonbtest;

    key     │   value    
────────────┼────────────
 city       │ Mesa
 mayor      │ John Smith
 county     │ Maricopa
 mayor      │ John Smith
 county     │ Maricopa
 other city │ Mesa
(6 rows)


Having this, getting the maximal length is a matter of a simple GROUP BY query:

SELECT key, max(char_length(value))
  FROM (SELECT (jsonb_each_text(data)).* FROM jsonbtest) t 
 GROUP BY key;

    key     │ max 
────────────┼─────
 city       │   4
 other city │  27
 county     │   8
 mayor      │  13
(4 rows)


See all this at work on dbfiddle.  See what else you cana use for processing your JSONB values in the docs.