title: How to translate groovy map to json
body: <p>I have the below code in Jenkins pipeline:</p>
  
  <pre><code>stage ("distribution"){
              steps{
                  script{
                      def rules = [
                              service_name: "core", 
                              site_name: "*", 
                              city_name: "*", 
                              country_codes: ["*"]
                  ]
                      amd_distribution_distribute_bundle distribution_rules: rules
                      }
                  }
              }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>{
    "distribution_rules": [
      {
        "service_name": "core*",
        "site_name": "*",
        "city_name": "*",
        "country_codes": ["*"]
      }
    ]
  }
  </code></pre>
  
  <p>I tried the below command but it didn't help:</p>
  
  <pre><code>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)
  }
  </code></pre>