com.alibaba.fastjson2.support.odps.JSONExtract2 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson2-extension Show documentation
Show all versions of fastjson2-extension Show documentation
Fastjson is a JSON processor (JSON parser + JSON generator) written in Java
The newest version!
package com.alibaba.fastjson2.support.odps;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.JSONReader;
import com.aliyun.odps.udf.UDF;
public class JSONExtract2
extends UDF {
public String evaluate(String json, String path) {
if (json == null || json.isEmpty()) {
return null;
}
Object result = null;
try (JSONReader jsonReader = JSONReader.of(json)) {
result = JSONPath.of(path)
.extract(jsonReader);
} catch (Throwable ignored) {
// ignored
}
if (result == null) {
return null;
}
try {
return JSON.toJSONString(result);
} catch (Exception e) {
return null;
}
}
}