groovy 正在从多个数组列表中准备对象的JSON数组

vmjh9lq9  于 2022-11-01  发布在  其他
关注(0)|答案(2)|浏览(182)

我对Groovy脚本很陌生,想从下面的JSON输入中构建一个JSON输出。请帮助!
我的JSON输入如下所示:

{
  "id":"1222",
  "storageNode": {
    "uuid": "22255566336",
    "properties": {
      "BuinessUnit": [
        "Light",
        "Fan",
        "Watch"
        ],
      "Contact": [
        "abc@gmail.com",
        "fhh@gmail.com"
        ],
      "Location": [
        "Banglore",
        "Surat",
        "Pune"
        ]
    }
  }
}

预期输出:

[
{
  "BuinessUnit": "Light",
  "Contact": "abc@gmail.com",
  "Location": "Banglore"
},
{
  "BuinessUnit": "Fan",
  "Contact": "fhh@gmail.com",
  "Location": "Surat"
},
{
  "BuinessUnit": "Watch",
  "Contact": "",
  "Location": "Pune"
}
]

请注意,如果任何数组与值计数不匹配,则该值计数将始终是最后一个,在这种情况下,必须填充空值("")。可以引用“BusinessUnit”对象进行数组大小验证。
我的程式码如下所示:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;

def Message processData(Message message) {    
    //Body 
    def body = message.getBody(String.class);    
    def jsonSlurper = new JsonSlurper()
    def list = jsonSlurper.parseText(body)
    String temp

    def BU = list.storageNode.properties.get("BusinessUnit")

    def builder = new JsonBuilder(
        BU.collect {
            [
                BusinessUnit: it

            ]
        }
    )
    message.setBody(builder.toPrettyString())
    return message
}

它只返回以下内容:

[
    {
        "BusinessUnit": "Light"
    },
    {
        "BusinessUnit": "Fan"
    },
    {
        "BusinessUnit": "Watch"
    }
]

现在我该如何添加其他部分呢?请帮助!

ki0zmccv

ki0zmccv1#

我提出了下面的解决方案,将源JSON字符串转换为目标JSON字符串:

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def json = '''
{
  "id":"1222",
  "storageNode": {
    "uuid": "22255566336",
    "properties": {
      "BusinessUnit": [
        "Light",
        "Fan",
        "Watch"
        ],
      "Contact": [
        "abc@gmail.com",
        "fhh@gmail.com"
        ],
      "Location": [
        "Banglore",
        "Surat",
        "Pune"
        ]
    }
  }
}
'''

println convert(json)

String convert(String json) {
    def list = new JsonSlurper().parseText(json)
    List<String> units = list.storageNode.properties.BusinessUnit
    List<String> contacts = list.storageNode.properties.Contact
    List<String> locations = list.storageNode.properties.Location
    def result = []
    units.eachWithIndex { unit, int index ->
        result << [
            BusinessUnit: unit,
            Contact     : contacts.size() > index ? contacts[index] : '',
            Location    : locations.size() > index ? locations[index] : '',
        ]
    }
    return new JsonBuilder(result).toPrettyString()
}

我已经省略了从消息中获取字符串和将转换后的JSON打包到消息中的逻辑。我希望它能帮助你继续前进。如果你需要进一步的帮助,请让我知道。

cdmah0mi

cdmah0mi2#

您可以使用内置的Groovy工具,如transpose()

import groovy.json.*

def json = new JsonSlurper().parseText '''{   "id":"1222",   "storageNode": {     "uuid": "22255566336",     "properties": {       
   "BuinessUnit": [         "Light",         "Fan",         "Watch"         ],      
   "Contact": [         "abc@gmail.com",         "fhh@gmail.com"         ],       
   "Location": [         "Banglore",         "Surat",         "Pune"         ]     }   } }'''

def names = json.storageNode.properties*.key
def values = json.storageNode.properties*.value

int maxSize = values*.size().max()
// pad lists with trainiling spaces
values.each{ v -> ( maxSize - v.size() ).times{ v << '' } }

def result = values.transpose().collect{ tuple -> [ names, tuple ].transpose().collectEntries{ it } }

assert result.toString() == '[[BuinessUnit:Light, Contact:abc@gmail.com, Location:Banglore], [BuinessUnit:Fan, Contact:fhh@gmail.com, Location:Surat], [BuinessUnit:Watch, Contact:, Location:Pune]]'

这段代码可以处理storageNode.properties下的所有内容。

相关问题