I'm one of the authors. For future reference, in the upcoming SRU2018 release (scheduled for October) we have revamped all the JSON API in all model abstraction layers.
The toJson in SwiftMessage object still produces plain name/value tuples for the Tags. However, in the MTnnn classes, the toJson uses Field to produce JSON with business labels such as:
{ "name": "90A",
"qualifier": "DEAL",
"percentageTypeCode": "PRCT",
"price": "102,713552"
},
{ "name": "36B",
"qualifier": "ESTT",
"quantityTypeCode": "AMOR",
"quantity": "7999999,573"
},
{ "name": "97A",
"qualifier": "SAFE",
"account": "0123-0123456-55formatted
}
Besides the JSON, you can iterate the fields and print formated name and values with the available getLabel and getValueDisplay methods.
For example:
Locale locale = Locale.getDefault();
SwiftMessage sm = SwiftMessage.parse("{1:F01BACOARB1A0B20000000000}{2:I103ADRBNL21XXXXU2}{3:{108:FOOB3926BE868XXX}}{4:\n" +
":20:REFERENCE\n" +
":23B:CRED\n" +
":32A:180730USD1234567,89\n" +
":50A:/12345678901234567890\n" +
"CFIMHKH1XXX\n" +
":59:/12345678901234567890\n" +
"JOE DOE\n" +
"MyStreet 1234\n" +
":71A:OUR\n" +
"-}");
System.out.println("Sender: " + sm.getSender());
System.out.println("Receiver: " + sm.getReceiver() + "\n");
for (Tag tag : sm.getBlock4().getTags()) {
Field field = tag.asField();
System.out.println(Field.getLabel(field.getName(), "103", null, locale));
System.out.println(field.getValueDisplay(locale) + "\n");
}
Will produce this output:
Sender: BACOARB1A0B2
Receiver: ADRBNL21XXXX
Sender's Reference
REFERENCE
Bank Operation Code
CRED
Value Date/Currency/Interbank Settled Amount
Jul 30, 2018 USD 1,234,567.89
Ordering Customer
12345678901234567890 CFIMHKH1XXX
Beneficiary Customer
12345678901234567890 JOE DOE MyStreet 1234
Details of Charges
OUR
Where components are split and formatted for the locale.
And if you also need labels per component, you can further iterate the components like this:
for (Tag tag : sm.getBlock4().getTags()) {
Field field = tag.asField();
System.out.println("\n" + Field.getLabel(field.getName(), "103", null, locale));
for (int component = 1 ; component <= field.componentsSize() ; component++) {
if (field.getComponent(component) != null) {
System.out.print(field.getComponentLabel(component) + ": ");
System.out.println(field.getValueDisplay(component, locale));
}
}
}
Pruducing this other output:
Sender's Reference
Reference: REFERENCE
Bank Operation Code
Type: CRED
Value Date/Currency/Interbank Settled Amount
Date: Jul 30, 2018
Currency: USD
Amount: 1,234,567.89
Ordering Customer
Account: 12345678901234567890
BIC: CFIMHKH1XXX
Beneficiary Customer
Account: 12345678901234567890
Name And Address: JOE DOE
Name And Address 2: MyStreet 1234
Details of Charges
Code: OUR
Finally, if you are interested, the Integrator library from Prowide includes out-of-the-box printout visitors to produce HTML, TXT, and XML including structured sequences and BIC expanded with the institution information. You may ask Prowide for a trial.