Michal Testowy
Michal Testowy

Reputation: 11

soapui update json request from script

I can't find any solution for my issue. Many I've tried doesn't work. Could someone help me?

I have a JSON like this:

{
"device": {
  "sdk_revision": "dev",
  "sdk_type": "android",
  "app_id": "0518101906",
  "app_version": "0.0.0"
},
"request": {}
}

in my request step in soapUI (RestProject).

I'd like to change the app_id value in this JSON, so in my groovy script I do:

import groovy.json.JsonSlurper

def today = new Date()
appId = today.format("MMddHHmmss")

def extractSelectionJson(String from) {
    def slurper = new JsonSlurper()
    def holderData = slurper.parseText(context.expand('${'+ from +'#Request}'))

appIdKey = holderData["device"]["app_id"]
appIdKey = appId
}

extractSelectionJson("SessionCreate")`

appId changed only locally, at my json request I have still "app_id": "0518101906" although I've tried setPropertyValue(), updateProperty() (maybe not right way).

Upvotes: 1

Views: 1056

Answers (1)

SiKing
SiKing

Reputation: 10329

For something simple like this, you can use just a one-liner:

{
  "device": {
    "sdk_revision": "dev",
    "sdk_type": "android",
    "app_id": "${=String.format('%tm%td%tH%tM%tS'
        , new Date()
        , new Date()
        , new Date()
        , new Date()
        , new Date())}",
    "app_version": "0.0.0"
  },
  "request": {}
}

If you need details for the String.format(), it is in the documentation.

Upvotes: 2

Related Questions