如何在Groovy中将JSON数据中的一个值替换为另一个值?

huangapple go评论58阅读模式
英文:

How to replace a value with another in json data in Groovy?

问题

I am trying to replace API Data with "All" in Emp_Id field in data in json. And then make rows with every data of API.

API DATA: I have stored this data in "Response" attribute in ExtractText in Apache Nifi.

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

data:

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

Expected Result

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

This is tried in ecmaScript but I want it to be in groovy because these functions are not working in executeScript of NIFI and giving error."java.lang.assertionError: geberating bytecode".
Or if any other approach for converting this data.

def response = [
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470, 1]]
]

def template = [
    "Emp_Id": "All",
    "Emp_loc": 523,
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_dept": "Accountant"
]

def result = response.data.collect { id, _ ->
    template.collectEntries { k, v ->
        [(k): (v == "All") ? id.toString() : v]
    }
}

println(result)
英文:

I am trying to replace API Data with "All" in Emp_Id field in data in json. And then make rows with every data of API.

API DATA: I have stored this data in "Response" attribute in ExtractText in Apache Nifi.

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

//This API data is more, I have taken only 6 for reference but in real its almost 500-600
//(ignore 0 and 1 that is not required).

data:

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

Expected Result

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

This is tried in ecmaScript but I want it to be in groovy because these functions are not working in executeScript of NIFI and giving error."java.lang.assertionError: geberating bytecode".
Or if any other approach for converting this data.

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

const template = {
    "Emp_Id": "All",
    "Emp_loc": 523,
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_dept": "Accountant"
};

const result = response.data.map(([id]) =>
    Object.fromEntries(Object.entries(template).map(([k, v]) =>
        [k, v === "All" ? id : v]
    ))
);
    
console.log(result);

答案1

得分: 0

[
    {"Emp_Id": 123, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"},
    {"Emp_Id": 124, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"},
    {"Emp_Id": 446, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"},
    {"Emp_Id": 617, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"},
    {"Emp_Id": 620, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"},
    {"Emp_Id": 470, "Emp_loc": 523, "Emp_dept": "Accountant", "Emp_sub_dept": "Finance"}
]
英文:
import groovy.json.JsonBuilder

def response = [
    "status": "success",
    "data": [[123, 0], [124, 0], [446, 0], [617, 1], [620, 0], [470 ,1]]
]

def template = [
    "Emp_Id": "All",
    "Emp_loc": 523,
    "Emp_dept": "Management",
    "Emp_sub_dept": "Finance",
    "Emp_dept": "Accountant"
]

def result = response.data.collect{ i -> 
        template.collectEntries{ k,v -> 
            [k, v=="All" ? i[0] : v] 
        } 
    }
 
println new JsonBuilder(result).toPrettyString()

https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Map.html#collectEntries(groovy.lang.Closure)

https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#collect(groovy.lang.Closure)

huangapple
  • 本文由 发表于 2023年5月21日 01:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定