Convert Values and Records to JSON and XML
Updated: Oct 5, 2026
This example shows how to turn records, lists and single values into JSON and XML text. Every ValueNode can render itself with toJson() and toXml(), and Record is itself a ValueNode. Plain Java objects are wrapped with ValueNode.from(): collections and arrays become an ArrayValue, any other object becomes a SingleValue, and a value that already is a ValueNode is returned as-is, so a record is never wrapped.
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook;
import java.math.BigDecimal;
import java.util.Arrays;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.ValueNode;
public class ConvertValuesAndRecordsToJsonAndXml {
public static void main(String[] args) {
Record record = new Record()
.setField("Account", 101)
.setField("Name", "Keanu Reeves");
System.out.println(record.toJson());
System.out.println(record.toXml());
ValueNode> tags = ValueNode.from(Arrays.asList("vip", "early-adopter"));
System.out.println(tags.toJson());
System.out.println(tags.toXml());
ValueNode> balance = ValueNode.from(new BigDecimal("9315.45"));
System.out.println(balance.toJson());
System.out.println(balance.asSingleValue().getValueAsString());
}
}
Code Walkthrough
- A
Recordis created with an integerAccountfield and a stringNamefield.toJson()renders it as a JSON object, keeping the number unquoted, andtoXml()renders it as a<document>element with one child element per field. ValueNode.from(Arrays.asList(...))wraps the list in anArrayValue.toJson()renders a JSON array andtoXml()a<document>element with one<single_value>child per item.ValueNode.from(new BigDecimal("9315.45"))wraps the number in aSingleValue.toJson()prints the bare number, andasSingleValue().getValueAsString()returns its text form.
Console Output
{"Account":101,"Name":"Keanu Reeves"}
<?xml version='1.0' encoding='UTF-8'?><document><Account>101</Account><Name>Keanu Reeves</Name></document>
["vip","early-adopter"]
<?xml version='1.0' encoding='UTF-8'?><document><single_value>vip</single_value><single_value>early-adopter</single_value></document>
9315.45
9315.45
