jenkins - Looping in a C-style For loop instead of using each -


how can loop through json file using for loop in groovy? able .each in situation/bug cannot use .each loops.

the json file being read , parsed object.

the json looks this:

{     "workflows1": {         "name": "/wf_multifolder",         "file": "release1/wf_multifolder.xml",         "foldernames": {             "multifolder": "{{multifolder}}",             "agent1": "{{agentx}}"         }     },     "workflows2": {         "name": "/wf_multifolder",         "file": "release1/wf_multifolder.xml",         "foldernames": {             "multifolder": "{{multifolder}}",             "agent1": "{{agentx}}"         }     } } 

note: can modify json file, if need make process simpler.. try loop throgh , extract values keys.

so given json in string so:

def jsontext = '''{     "workflows1": {         "name": "/wf_multifolder",         "file": "release1/wf_multifolder.xml",         "foldernames": {             "multifolder": "{{multifolder}}",             "agent1": "{{agentx}}"         }     },     "workflows2": {         "name": "/wf_multifolder",         "file": "release1/wf_multifolder.xml",         "foldernames": {             "multifolder": "{{multifolder}}",             "agent1": "{{agentx}}"         }     } }''' 

you can do:

import groovy.json.*  def json = new jsonslurper().parsetext(jsontext)  for(entry in json) {     println "$entry.key has file $entry.value.file" } 

to print:

workflows1 has file release1/wf_multifolder.xml workflows2 has file release1/wf_multifolder.xml 

Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -