in inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/json/JsonInsertFunction.java [78:201]
private static String jsonInsert(String json, ArrayList<Object> pathValuePairs) {
if (json == null || pathValuePairs == null || pathValuePairs.size() % 2 != 0) {
return null;
}
JSONObject jsonObject;
try {
jsonObject = JSON.parseObject(json);
} catch (Exception e) {
// Invalid JSON document
return null;
}
for (int i = 0; i < pathValuePairs.size(); i += 2) {
String path = (String) pathValuePairs.get(i);
Object value = pathValuePairs.get(i + 1);
if (path == null || value == null) {
return null;
}
if (path.contains("*") || path.contains("**")) {
throw new IllegalArgumentException("Invalid path expression: " + path);
}
List<Object> tokens = parsePath(path);
if (tokens.isEmpty()) {
continue;
}
Object current = jsonObject;
Object parent = null, currentToken = null;
for (int j = 0; j < tokens.size() - 1; j++) {
Object token = tokens.get(j);
parent = current;
currentToken = token;
if (token instanceof String) {
String key = (String) token;
if (!(current instanceof JSONObject)) {
JSONObject newObj = new JSONObject();
if (parent instanceof JSONObject) {
((JSONObject) parent).put((String) currentToken, newObj);
} else if (parent instanceof JSONArray) {
((JSONArray) parent).set((Integer) currentToken, newObj);
}
current = newObj;
}
JSONObject jsonObj = (JSONObject) current;
Object next = jsonObj.get(key);
if (next == null) {
Object nextToken = tokens.get(j + 1);
next = nextToken instanceof Integer ? new JSONArray() : new JSONObject();
jsonObj.put(key, next);
}
current = next;
} else if (token instanceof Integer) {
int index = (Integer) token;
if (!(current instanceof JSONArray)) {
JSONArray newArr = new JSONArray();
newArr.add(current);
if (parent instanceof JSONObject) {
((JSONObject) parent).put((String) currentToken, newArr);
} else if (parent instanceof JSONArray) {
((JSONArray) parent).set((Integer) currentToken, newArr);
}
current = newArr;
}
JSONArray jsonArr = (JSONArray) current;
if (jsonArr.size() <= index) {
index = jsonArr.size();
}
Object next = jsonArr.get(index);
if (next == null) {
Object nextToken = tokens.get(j + 1);
next = nextToken instanceof Integer ? new JSONArray() : new JSONObject();
jsonArr.set(index, next);
}
current = next;
}
}
Object lastToken = tokens.get(tokens.size() - 1);
if (lastToken instanceof String) {
String key = (String) lastToken;
if (!(current instanceof JSONObject)) {
JSONObject newObj = new JSONObject();
if (parent instanceof JSONObject) {
((JSONObject) parent).put((String) currentToken, newObj);
} else if (parent instanceof JSONArray) {
((JSONArray) parent).set((Integer) currentToken, newObj);
}
current = newObj;
}
JSONObject jsonObj = (JSONObject) current;
if (!jsonObj.containsKey(key)) {
jsonObj.put(key, parseValue(value));
}
} else if (lastToken instanceof Integer) {
int index = (Integer) lastToken;
if (!(current instanceof JSONArray)) {
JSONArray newArr = new JSONArray();
newArr.add(current);
if (parent instanceof JSONObject) {
((JSONObject) parent).put((String) currentToken, newArr);
} else if (parent instanceof JSONArray) {
((JSONArray) parent).set((Integer) currentToken, newArr);
}
current = newArr;
}
JSONArray jsonArr = (JSONArray) current;
if (index >= jsonArr.size() || jsonArr.get(index) == null) {
if (jsonArr.size() <= index) {
index = jsonArr.size();
}
jsonArr.set(index, parseValue(value));
}
}
}
return jsonObject.toJSONString();
}