Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Input mode: Scripted (JSON Rest)

jngojngo
jngojngo Posts: 46

I am planning to use the scripted(input mode) REST. Here's an simple example of JSON message Example1. What I would like to do is to use context.getValue as part of the JSON message shown in example2. I have added the script to pull json message below. My question is there a way to get the value context.getValue as part of the json message

simple JSON message Example1:
{
"Request": {
"RequestHeader": {
"Data": {
"ChannelID": "channel",
"TerminalID": "terminal"
}
}
}
}

simple JSON message Example2 version1: (does not work as slurper will fail)

{
"Request": {
"RequestHeader": {
"Data": {
"ChannelID": "context.getValue('dataSource','channel')",
"TerminalID": "context.getValue('dataSource',terminal)"
}
}
}
}
simple JSON message Example2 version2: (does not get the value it's output is the same)

{
"Request": {
"RequestHeader": {
"Data": {
"ChannelID": "${context.getValue('dataSource','channel')}",
"TerminalID": "${context.getValue('dataSource',terminal)}"
}
}
}
}

Script
import com.parasoft.api.*;
import groovy.json.JsonOutput
import groovy.json.JsonBuilder;
import groovy.json.*;

public buildHistInquiry( context ){
myJSON = "C:\SOA_Workspace\test.json"
myFileReader = new File(myJSON).text
slurper = new JsonSlurper()
myJsonRqst = slurper.parseText( myFileReader )

String JSON = new JsonBuilder(myJsonRqst).toString()

return JSON

}

Comments

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    If you are attempting to access a data source column from a literal payload you could use the literal mode, instead of scripted and then use ${} to reference the column by name.

  • jngojngo
    jngojngo Posts: 46

    thanks for the input. I would also like to removed some elements that is not needed as part of the request message.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    I would recommend changing Input Mode to Literal, enter "C:\SOA_Workspace\test.json" for Input file, then select the "Resolve environment variables" box which should actually resolve all types of variables. In your test.json file, you would just use normal variable ${column_name} syntax. If you need to do post processing of the JSON from a script, then you can chain an Extension tool to your REST Client's outgoing request traffic output. The script in your Extension tool will receive the original message as input (with data source column variables already resolved) and your script would just need to return the modified message as output. You may recall me mentioning this to you earlier in this other thread: scripting remove empty elements