1

In Groovy I have to update values in JSON payload and make an API call. I am running into challenges while updating payload as the fields are embedded in backslash. Is there a simpler way to directly update the servers in below payload i.e update 1. JSON payload to 2. Updated JSON payload (updating name and host values).

1. JSON payload:

    {
    "environment": "dev",
    "config": "Create",
    "configType": "Server",
    "ServerName": "",
    "Servers": "[\\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
    "tsHost": "",
    "tsPort": "",
    "tsSSLOption": "1-way"
    }

2. Updated JSON payload:

    {
    "environment": "dev",
    "config": "Update",
    "configType": "Server",
    "ServerName": "",
    "Servers": "[\\{\"name\":\"Server-test_2\",\"host\":\"test123.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
    "tsHost": "",
    "tsPort": "",
    "tsSSLOption": "1-way"
    }

Tried below (losing backslash in conversion process):

Code:

    def json = $/ {
    "environment": "dev",
    "config": "Create",
    "configType": "Server",
    "ServerName": "",
    "Servers": "[\\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
    "tsHost": "",
    "tsPort": "",
    "tsSSLOption": "1-way"
    } 
/$
    def parser = new JsonSlurper()
    def jsonResp = parser.parseText(json)
    println(jsonResp.Servers)
    jsonResp.Servers.name = "Server-test_2"
    jsonResp.Servers.host = "test123.com"
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
RATI
  • 13
  • 3
  • The fields are _not_ embedded in backslash. The backslashes escape the following characters. See [Introducing JSON, string](https://www.json.org/json-en.html). – Gerold Broser Aug 22 '21 at 00:27
  • 1
    Can you post your Groovy code. Also why are there escape characters before a curly bracket? If you post the Groovy code I will troubleshoot it for you. But to be clear the backslashes are just escape characters. Also, is this in Jenkins or just groovy? Your tag says Jenkins – Chris Maggiulli Aug 22 '21 at 01:01
  • I second Chris. If this is in Jenkins in which project type is it? – Gerold Broser Aug 22 '21 at 13:11
  • @ChrisMaggiulli I am using groovy script in pipeline to implement a function. (This is just groovy, sorry for the confusion). API needs data in the format posted in the payloads. Updated post with code in code section. – RATI Aug 22 '21 at 21:03
  • **Code:** import java.io.* import groovy.json.JsonSlurper def json = $/ { "environment": "dev", "config": "Create", "configType": "Server", "ServerName": "", "Servers": "[\\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]", "tsHost": "", "tsPort": "", "tsSSLOption": "1-way" } /$ def parser = new JsonSlurper() def jsonResp = parser.parseText(json) println(jsonResp.Servers) jsonResp.Servers.name = "Server-test_2" jsonResp.Servers.host = "test123.com" – RATI Aug 22 '21 at 21:08
  • @GeroldBroser, I am using groovy script in pipeline to implement a function. (This is just groovy, sorry for the confusion). – RATI Aug 22 '21 at 21:11
  • Please add the code to your question by using the _Edit_ link below the tags under your question. – Gerold Broser Aug 22 '21 at 21:32
  • 1
    You’re having an issue because the value of the servers property is a string, not an array of json objects. You need to convert the string into an array of json objects before you can interact with it as if it were json. It has nothing to do with the escape characters. Then, depending on the API, you probably need to convert it back to a string at which point the escape characters should be managed for you. That is if I’m understanding you correctly, which I’m still not 100% sure of – Chris Maggiulli Aug 22 '21 at 22:14

1 Answers1

1

Servers is a string in your initial json - you have to parse it

import groovy.json.*

def json = $/ {
    "environment": "dev",
    "config": "Create",
    "configType": "Server",
    "ServerName": "",
    "Servers": "[{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
    "tsHost": "",
    "tsPort": "",
    "tsSSLOption": "1-way"
    } 
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)

def servers = parser.parseText(jsonResp.Servers)
servers[0].name="Server-test_2"
servers[0].host="test123.com"
jsonResp.Servers = JsonOutput.toJson(servers)
json = JsonOutput.prettyPrint(JsonOutput.toJson(jsonResp))
daggett
  • 26,404
  • 3
  • 40
  • 56
  • To be precise (for the OP): In this answer's code `Servers` is a string that contains a definition of an [array `[...]`](https://www.json.org/json-en.html#array) that contains one JSON object `{...}` as only element. But what's the the escaped backslash in the question's code? An error? – Gerold Broser Aug 23 '21 at 15:34
  • 1
    you are right. we don't have any info where 1-st json is coming from. if it's just a static data - definitely it's typo. – daggett Aug 23 '21 at 17:08
  • Thank you @daggett. Worked with dev team and modified the payload. – RATI Aug 27 '21 at 14:43