This topic describes how to use a Java UDF to convert data types to JSON STRING.

During data usage, you often need to convert data types to JSON STRING. For example, if the data type is ARRAY, you may need to convert it to JSON STRING for data import and download because Tunnel does not support uploading and downloading data of complex types.

UDF used to convert data types to JSON STRING

toJSONString()
  • Description: used to convert a MaxCompute object (field) to a JSON string, along with the necessary string escape. This UDF can work with PutJsonValues.
  • Parameters: The types of input parameters cannot be DATETIME.

UDF example

  • Resource upload

    Due to sandbox limits, to run this function in Alibaba Cloud MaxCompute, you need to upload the fastjson.JSON and UDF JAR packages to MaxCompute as resources and reference the two packages when you create a function in DataWorks. You can visit MVN to download the fastjson.JSON package.

  • Function registration

    After ToJsonStrin.java passes the test, register it as a function.

    In this example, ODPSUDF-1.0-SNAPSHOT2.jar is the JAR package generated by packaging the example code of the UDF used to convert data types to JSON STRING. fastjson-1.2.28.odps.jar is the fastjson.JSON package.

  • Example
    SELECT tojson(array(2.0,3.0,4.0)) FROM dual;
    Note tojson is the function you register by using DataWorks or IntelliJ IDEA.
    The result is as follows:
    [2.0,3.0,4.0]

UDF code example

import com.aliyun.odps.udf.UDF; // Alibaba Cloud UDF.
import java.math.BigDecimal;
import com.alibaba.fastjson.JSON;  // The fastjson.JSON class is introduced.
import java.util.Arrays;
import java.util.List;

public class ToJsonString extends UDF {
// The following code calls the toJSONString method of fastjson.JSON to convert various data types to JSON STRING. You can add more data types as needed.
    public String evaluate(String obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(Long obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(Double obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(Boolean obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(BigDecimal obj) {
        return JSON.toJSONString(obj);
    }
// In MaxCompute, the ARRAY type corresponds to the LIST type in Java UDFs. For example, the Array <double> type matches the List <Double> type in a UDF.
    public String evaluate(List<Double> obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(double[] obj) {
        return JSON.toJSONString(obj);
    }
    public String evaluate(int[] obj) {
        return JSON.toJSONString(obj);
    }
}

UDF unit testing

// The following code is used to check whether the data type conversion takes effect. You can comment out the code when you use the function.
    public static void main(String[] args) {
        //System.out.println(new ToJsonString().evaluate(null));//MaxCompute can call this method but Java cannot.
        System.out.println(new ToJsonString().evaluate("Chinese\t"));
        System.out.println(new ToJsonString().evaluate(123L));
        System.out.println(new ToJsonString().evaluate(1.123));
        System.out.println(new ToJsonString().evaluate(true));
        //System.out.println(new ToJsonString().evaluate(new Date()));// UDFs and UDAFs do not support the DATETIME type.
        System.out.println(new ToJsonString().evaluate(BigDecimal.TEN));
        System.out.println(new ToJsonString().evaluate(Arrays.asList(1.0,2.0,3.0,4.0)));
        double[] args1 = {1,2,3,4};
        System.out.println(new ToJsonString().evaluate(args1));
       }
    }