从数据库列生成 json 格式的值



我在table中有一个名为canonicalNamecolumn,其中包含Vision Canadaapple computers等值。 表包含大约 10k 条记录。我必须将这些值以以下格式传递给 API。我正在使用oracle数据库表。表名为Organization

{
"add": [        
{
"canonicalName": "Vision Canada"

},
{
"canonicalName": "apple computers",                       
}
]
}

你能建议我如何在 Java 中生成相同的内容吗

在不知道表格来自哪个数据源的情况下,很难给出一个真正具体的答案,但这是我在 javascript 中最好的镜头:

const columnName = 'canonicalName' // Get this once from your table
const arrayOfColumnValues = ['Vision Canada', 'Apple Computer'] // However you get your values out of the table they should be in an array like so
// This array will hold all of the { 'canonicalName' : 'x' } fields in the 'Add': [] you need to pass to the API
const arrayOfColumnObjects = []
// Here we iterate through each value from the column in the table and send it to the object array.
// {[columnName]: value} sets the key to the VARIABLE 'columnName'.
// This is different than saying {columnName: value} which would set the key to the STRING "columnName".
arrayOfColumnValues.forEach(value => arrayOfColumnObjects.push({[columnName]: value}))
// Then we wrap the arrayOfColumnObjects in the object we need to send it to the API like so
const apiJsonObject = {
Add: arrayOfColumnObjects
}
// Of course here you would return it instead of logging
console.log(apiJsonObject)

JS小提琴

最新更新