[英]Parse Data Using Jenkins Groovy Pipeline Script
I am retrieving JSON object from a URL using httpRequest
in a groovy script. 我正在使用Groovy脚本中的
httpRequest
从URL检索JSON对象。
pipeline {
agent any
stages {
stage ('Extract Data') {
steps {
script {
def response = httpRequest \
authentication: 'user', \
httpMode: 'GET', \
url: "https://example.com/data"
writeFile file: 'output.json', text: response.content
def data = readFile(file: 'output.json')
def details = new groovy.json.JsonSlurperClassic().parseText(data)
echo "Data: ${details.fields.customfield}"
}
}
}
}
}
I am interested in the customfield
string. 我对
customfield
字符串感兴趣。 The format of the string is: 字符串的格式为:
Application!01.01.01 TestSuite1,TestSuite2,TestSuite3,TestSuite4 Product!01.01.01,Product2!01.01.02
I would like to parse the string into 3 data sets: 我想将字符串解析为3个数据集:
However, I am not sure how to do this. 但是,我不确定如何执行此操作。 Are there any Jenkins Groovy libraries that I can use to do this in a declarative pipeline?
我可以在声明式管道中使用任何Jenkins Groovy库吗?
EDIT Based on the answer below I can see that I can make a map in the following way: 编辑根据以下答案,我可以看到我可以通过以下方式制作地图:
def applications = groups[0].split(',').collect { it.split('!') }.collectEntries { [(it):it] }
In the example I have: 在示例中,我有:
application = [Application: Application]
How do I get: application = [Application: 01.01.01] 我如何获得:应用程序= [应用程序:01.01.01]
EDIT2 Note the following output: EDIT2请注意以下输出:
def applications = groups[0].split(',').collect { it.split('!') }
[[Application, 01.01.01]]
There're no libraries I'm aware of that will have functionality to parse the data but, since you know the format of the data it's easy to parse them manually . 据我所知,没有一个库具有解析数据的功能,但是,由于您知道数据的格式,因此很容易手动解析它们。
There are 3 groups in the input ( applications
, suites
, products
) separated by a 输入中有3组(
applications
, suites
, products
),由 character.
字符。 To get the groups you need:
要获得组,您需要:
def input = "Application!01.01.01 TestSuite1,TestSuite2,TestSuite3,TestSuite4 Product!01.01.01,Product2!01.01.02"
def groups = input.split(' ')
To process the applications you need to split group 0 with ,
character (just in case there are many applications). 要处理你需要拆分组0的应用程序
,
字符(以防万一有很多的应用程序)。 You got a list of pairs in format: name!version
. 您会得到以下格式的成对列表:
name!version
。 Every pair must be splitted with !
每对必须分开
!
, so you get a list of lists in format: [[name, version]]
. ,因此您将获得格式为
[[name, version]]
的列表列表。 From the last structure it's easy to create a map. 从最后的结构很容易创建地图。 All steps together:
所有步骤合在一起:
def applications = groups[0].split(',').collect { it.split('!') }.collectEntries { [(it[0]):it[1]] }
Getting the list of suites is easy, just split group 1 with ,
character: 获取套件列表很容易,只需将第一个分组用
,
字符分开:
def suites = groups[1].split(',')
Finally, products are analogical to the list of applications but this time group 2 should be used: 最后,产品类似于应用程序列表,但是应该使用第二组:
def products = groups[2].split(',').collect { it.split('!') }.collectEntries { [(it[0]):it[1]] }
I found a method. 我找到了一种方法。 Groovy can convert the values of an Object array and convert them into a map with the
toSpreadMap()
. Groovy可以转换一个Object数组的值,并使用
toSpreadMap()
将它们转换为一个映射。 However, the array must have an even number of elements. 但是,数组必须具有偶数个元素。
def appList = ['DevOpsApplication', '01.01.01']
def appMap = appList.toSpreadMap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.