Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate groovy map to json

I have the below code in Jenkins pipeline:

stage ("distribution"){
            steps{
                script{
                    def rules = [
                            service_name: "core", 
                            site_name: "*", 
                            city_name: "*", 
                            country_codes: ["*"]
                ]
                    amd_distribution_distribute_bundle distribution_rules: rules
                    }
                }
            }

As you can see, it's a map parameter. How can I convert it to the JSON file using Groovy code? At the end it should look like:

{
  "distribution_rules": [
    {
      "service_name": "core*",
      "site_name": "*",
      "city_name": "*",
      "country_codes": ["*"]
    }
  ]
}

I tried the below command but it didn't help:

import groovy.json.JsonBuilder
import groovy.json.JsonOutput

def call(Map parameters)
{
    def DISTRIBUTION_RULES = parameters.distribution_rules
    def json = new groovy.json.JsonBuilder()
    json rootKey: "${DISTRIBUTION_RULES}"
    writeFile file: 'rootKey', text: JsonOutput.toJson(json)
}
like image 705
arielma Avatar asked Nov 12 '19 22:11

arielma


People also ask

What is Groovy JSON JsonSlurper?

JsonSlurper. JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as maps, lists and primitive types like Integer , Double , Boolean and String .

Is Jenkins written in Groovy?

The Jenkinsfile is written using the Groovy Domain-Specific Language and can be generated using a text editor or the Jenkins instance configuration tab. The Declarative Pipelines is a relatively new feature that supports the concept of code pipeline.


1 Answers

There is no need to mix JsonBuilder and JsonOutput in your amd_distribution_distribute_bundle.groovy file. The JsonOutput.toJson(map) method takes a regular Map and translates it to the JSON object equivalent. By default it creates a flat single line file. If you expect to get so-called pretty print, you need to use combination of JsonOutput.prettyPrint(JsonOutput.toJson(map)).

Flat print

import groovy.json.JsonOutput

def call(Map parameters) {
    def DISTRIBUTION_RULES = parameters.distribution_rules

    writeFile file: 'rootKey', text: JsonOutput.toJson([distribution_rules: [DISTRIBUTION_RULES]])
}

Output:

$ cat rootKey
{"distribution_rules":[{"service_name":"core","site_name":"*","city_name":"*","country_codes":["*"]}]}%    

Pretty print

import groovy.json.JsonOutput

def call(Map parameters) {
    def DISTRIBUTION_RULES = parameters.distribution_rules

    writeFile file: 'rootKey', text: JsonOutput.prettyPrint(JsonOutput.toJson([distribution_rules: [DISTRIBUTION_RULES]]))
}

Output:

$ cat rootKey
{
    "distribution_rules": [
        {
            "service_name": "core",
            "site_name": "*",
            "city_name": "*",
            "country_codes": [
                "*"
            ]
        }
    ]
}%     
like image 64
Szymon Stepniak Avatar answered Oct 12 '22 17:10

Szymon Stepniak