如何在Java中替换json数据中的值?

vi4fp9gy  于 2023-06-07  发布在  Java
关注(0)|答案(2)|浏览(405)

我尝试在json中的数据Emp_Id字段中将API Data替换为“All”。然后用API的每个数据做行。我尝试在ecmascript,但我需要在Java中,因为这个Map功能是给错误的nifi。
API数据:我已经将此数据存储在Apache Nifi的ExtractText中的“Response”属性中。

response 
{
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [620, 0], [470 ,1]]
};

jsonData
{
    "Emp_Id": "All",
    "Emp_loc": "523",
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_sub_dept2": "Accountant"
};

预期结果

[
{
"Emp_Id":"123",
"Emp_loc":"523",
"Emp_dept":"Management",
"Emp_sub_dept":"Finance",
"Emp_sub_dept2":"Accountant"
},
{
"Emp_Id":"124",
"Emp_loc":"523",
"Emp_dept":"Management",
"Emp_sub_dept":"Finance",
"Emp_sub_dept2":"Accountant"
},
{
"Emp_Id":"446",
"Emp_loc":"523",
"Emp_dept":"Management",
"Emp_sub_dept":"Finance",
"Emp_sub_dept2":"Accountant"
},
{
"Emp_Id":"620",
"Emp_loc":"523",
"Emp_dept":"Management",
"Emp_sub_dept":"Finance",
"Emp_sub_dept2":"Accountant"
},
{
"Emp_Id":"470",
"Emp_loc":"523",
"Emp_dept":"Management",
"Emp_sub_dept":"Finance",
"Emp_sub_dept2":"Accountant"
}
]

这是在ecmaScript中尝试的,但我希望它是在Java中,因为这些函数在NIFI的executeScript中不起作用,并给出错误。geberating字节码”。或者是否有任何其他方法来转换该数据。
这是我试过的剧本

var InputStreamCallback = Java.type("org.apache.nifi.processor.io.InputStreamCallback")
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var OutputStreamCallback =  Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
var Set = Java.type("java.util.HashSet");
var Map = Java.type("java.util.HashMap");
var String = Java.type("java.lang.String");

var flowFile = session.get();
    
if (flowFile != null) {  
    var text = ''
    session.read(flowFile,
    new InputStreamCallback(function (inputStream) {
    text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);             
}));

var s3Data ={}
var apiResponse={}
var map = new Map();
    
var s3Data = JSON.parse(flowFile.getAttribute('jsonData'))
var apiResponse = JSON.parse(flowFile.getAttribute('response'))

var result = apiResponse.data.map(([id]) =>
Object.fromEntries(Object.entries(s3Data).map(([k, v]) =>
[k, v === "All" ? id : v]
))
);  
flowFile = session.write(flowFile,
    new OutputStreamCallback(function(outputStream) {
    outputStream.write(JSON.stringify(result).getBytes(StandardCharsets.UTF_8))
})
);
}

我也试过使用groovy,但我对Groovy的了解很少,所以我不能编写正确的ExecuteScript。

8ehkhllq

8ehkhllq1#

您可以尝试将其转换为JSONObject,然后将其替换为所需的值。
1.将响应字符串解析为简单的.JSONObject
1.从中获取数据值
1.将其转换为仅第一个数据值的列表。
1.循环遍历列表,并将jsonData的emp_id替换为新值。

String response = "{\r\n"
                + "    \"status\": \"success\",\r\n"
                + "    \"data\": [[123, 0], [124, 0], [446, 0], [620, 0], [470 ,1]]\r\n"
                + "}";
        String jsonData = "{\r\n"
                + "    \"Emp_Id\": \"All\",\r\n"
                + "    \"Emp_loc\": \"523\",\r\n"
                + "    \"Emp_dept\": \"Management\",\r\n"
                + "    \"Emp_sub_dept\": \"Finance\",\r\n"
                + "    \"Emp_sub_dept2\": \"Accountant\"\r\n"
                + "}";
        JSONParser parser = new JSONParser();
        JSONObject respObj = (JSONObject)parser.parse(response);
        List<JSONArray> numberList = (List)respObj.get("data");
        List<Long> replacableNumberList = numberList.stream().map(jsonArray -> (Long)jsonArray.get(0))
                .collect(Collectors.toList());
        
        
        List<JSONObject> outputList = new ArrayList<>();
        for (Long integer : replacableNumberList) {
            //if parsing before loop then dont forget to create new object before adding in the list
            JSONObject jsonObj = (JSONObject)parser.parse(jsonData);
            jsonObj.put("Emp_Id", integer);
            outputList.add(jsonObj);
        }
        outputList.forEach(System.out::println);
xzabzqsa

xzabzqsa2#

您可以尝试使用库 Josson & Jossons 来转换和连接两个数据集。
https://github.com/octomix/josson

反序列化、转换和连接

左连接<=<两个转换后的数据集,并匹配key

Josson response = Josson.fromJsonString(
    "{" +
    "    \"status\": \"success\"," +
    "    \"data\": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]" +
    "}");
Josson jsonData = Josson.fromJsonString(
    "{" +
    "    \"Emp_Id\": \"All\"," +
    "    \"Emp_loc\": \"523\"," +
    "    \"Emp_dept\": \"Management\"," +
    "    \"Emp_sub_dept\": \"Finance\"," +
    "    \"Emp_sub_dept2\": \"Accountant\"" +
    "}");
JsonNode node = new Jossons()
    .putDataset("response", response)
    .putDataset("jsonData", jsonData)
    .evaluateQuery("response->data@.[0].map(Emp_Id:?,key:'All'){key} <=< jsonData->field(key:Emp_Id,Emp_Id:){key}");
System.out.println(node.toPrettyString());

输出

[ {
  "Emp_Id" : 123,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
}, {
  "Emp_Id" : 124,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
}, {
  "Emp_Id" : 446,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
}, {
  "Emp_Id" : 617,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
}, {
  "Emp_Id" : 620,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
}, {
  "Emp_Id" : 470,
  "key" : "All",
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant"
} ]

演示response的转换

将每个data元素转移到单独的分支。对于每个分支,获取第一个数组元素,然后用元素Emp_Idkey构建一个对象。

JsonNode node = response.getNode("data@.[0].map(Emp_Id:?,key:'All')");
System.out.println(node.toPrettyString());

输出

[ {
  "Emp_Id" : 123,
  "key" : "All"
}, {
  "Emp_Id" : 124,
  "key" : "All"
}, {
  "Emp_Id" : 446,
  "key" : "All"
}, {
  "Emp_Id" : 617,
  "key" : "All"
}, {
  "Emp_Id" : 620,
  "key" : "All"
}, {
  "Emp_Id" : 470,
  "key" : "All"
} ]

演示jsonData的转换

添加值为Emp_Id的元素key,然后删除Emp_Id

JsonNode node = jsonData.getNode("field(key:Emp_Id,Emp_Id:)");
System.out.println(node.toPrettyString());

输出

{
  "Emp_loc" : "523",
  "Emp_dept" : "Management",
  "Emp_sub_dept" : "Finance",
  "Emp_sub_dept2" : "Accountant",
  "key" : "All"
}

相关问题