Have the following scenario, in my JSON response there is a value called visit which can have either 1 or more than 1 element. So, if the visit have 1 element then the default payload should be sent next POST request or if the visit have more than 1 element then I'm fetching a random value and want to update the payload and sent to the next POST request.
Let me elaborate,
Condition 1 [Have 1 element in the visit section]
Condition 2 [Have more than 1 element in the visit section]
JSON Response for Condition 1
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":201,"siteId":"20000"}],"subjects":[{"studyId":191,"siteRecid":201,"subjectRecid":245,"subjectNumber":"20002"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}
JSON Response for Condition 2
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":16521,"siteId":"11001"}],"subjects":[],"visits":[{"studyId":191,"visitSubmitName":"baseline","visitDisplayName":"Baseline","orderOfDisplay":10},{"studyId":191,"visitSubmitName":"cycle_1","visitDisplayName":"Cycle 1","orderOfDisplay":20}],"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"states":null,"modalities":null,"examDates":null,"series":null,"transferType":null,"customFolder":false,"customFile":false,"folderStructure":null,"fileStructure":null,"allSites":false,"allSubjects":true,"allVisits":false,"allStates":false,"allExamDates":false,"allModalities":false,"allSeries":false,"softEditOverride":false,"includePS":false,"includeSR":false,"includeRTStruct":false,"dicomTemplate":null,"errorMessage":null,"successMessage":null}
When I am in Condition 2, fetching random visitSubmitName,visitDisplayName,orderOfDisplay and updating in the payload.
Payload for condition 1:
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":201,"siteId":"20000"}],"subjects":[{"studyId":191,"siteRecid":201,"subjectRecid":245,"subjectNumber":"20002"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}
Payload for condition 2:
{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":16521,"siteId":"11001"}],"allSubjects":true,"states":null,"visits":[{"studyId":191,"visitSubmitName":"baseline","visitDisplayName":"Baseline","orderOfDisplay":10}],"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}
Only change in the payload from condition 1 is,
Solution I have tried so far is created a JSR223 post processer with the following code:
import groovy.json.JsonSlurper
Random rnd = new Random();
def jsonString = prev.getResponseDataAsString();
def jsonConvert = new JsonSlurper();
def object = jsonConvert.parseText(jsonString);
def studyCode = object.studyDTO.studyCode;
def visitS = object.visits.size().toString();
def visitSize = visitS?.isInteger() ? visitS.toInteger() : null
def visitNUM = rnd.nextInt(visitSize);
//def defaultPayload = "{"studyDTO":{"studyId":${studyID},"studyCode":"${study_name}"},"sites":[{"studyId":${studyID},"siteRecid":${siteRecID},"siteId":"${siteID}"}],"subjects":[{"studyId":${studyID},"siteRecid":${siteRecID},"subjectRecid":${subjectRecID},"subjectNumber":"${subjectNumber}"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}"
if (visitSize>1) {
def visitSubmitName = object.visits[visitNUM].visitSubmitName
def visitDisplayName = object.visits[visitNUM].visitDisplayName
def orderOfDisplay= object.visits[visitNUM].orderOfDisplay.toString();
//Change the payload with the three value
vars.put('payload',updatedPayload )
}
else {
vars.put('payload',defaultPayload )
}
And the next post sampler,
Error details:
Getting error with the defaultPayload declaration and need the logic for updating the payload.
Is the post sampler declared correctly?
Thanks, in advance
You either need to escape every quotation mark with a backslash
def defaultPayload = "def defaultPayload = '{\"studyDTO\":{\"studyId\":191,\"studyCode\":\"test_ispptest2\"},\"sites\":[{\"studyId\":191,\"siteRecid\":201,\"siteId\":\"20000\"}],\"subjects\":[{\"studyId\":191,\"siteRecid\":201,\"subjectRecid\":245,\"subjectNumber\":\"20002\"}],\"states\":null,\"allVisits\":true,\"modalities\":null,\"examDates\":null,\"series\":null,\"transferType\":null,\"sftpLocations\":[],\"dicomLocations\":[],\"fileSystemLocations\":[],\"rawFileSystemLocations\":[],\"customFolder\":null,\"folderStructure\":null,\"customFile\":null,\"fileStructure\":null,\"includePS\":null}'"
or use single quotation marks instead:
def defaultPayload = '{"studyDTO":{"studyId":191,"studyCode":"test_ispptest2"},"sites":[{"studyId":191,"siteRecid":201,"siteId":"20000"}],"subjects":[{"studyId":191,"siteRecid":201,"subjectRecid":245,"subjectNumber":"20002"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}'
No, you either need to use just ${payload} or if you prefer coding go for __groovy() function like ${__groovy(vars.get('payload'),)}
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy: What Is Groovy Used For?
Guess Dmitri has solved your issue, now coming to the logic. Before that, if I checked your payload for the post request are different. Please try the below code in your JSR223 post-processer.
The json update could be done through json.builder
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
Random rnd = new Random();
def jsonString = prev.getResponseDataAsString();
def jsonConvert = new JsonSlurper();
def object = jsonConvert.parseText(jsonString);
def studyCode = object.studyDTO.studyCode;
def visitS = object.visits.size().toString();
def visitSize = visitS?.isInteger() ? visitS.toInteger() : null
def visitNUM = rnd.nextInt(visitSize);
def defaultPayload = '{"studyDTO":{"studyId":${studyID},"studyCode":"${study_name}"},"sites":[{"studyId":${studyID},"siteRecid":${siteRecID},"siteId":"${siteID}"}],"subjects":[{"studyId":${studyID},"siteRecid":${siteRecID},"subjectRecid":${subjectRecID},"subjectNumber":"${subjectNumber}"}],"states":null,"allVisits":true,"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}';
def updatedPayLoad = "";
if (visitSize>1) {
def visitSubmitNameR = object.visits[visitNUM].visitSubmitName
def visitDisplayNameR = object.visits[visitNUM].visitDisplayName
def orderOfDisplayR = object.visits[visitNUM].orderOfDisplay.toString();
def newORDEROFDIS = orderOfDisplayR?.isInteger() ? orderOfDisplayR.toInteger() : null
updatedPayLoad = '{"studyDTO":{"studyId":${studyID},"studyCode":"${study_name}"},"sites":[{"studyId":${studyID},"siteRecid":${siteRecID},"siteId":"${siteID}"}],"allSubjects":true,"states":null,"visits":[{"studyId":${studyID},"visitSubmitName":"","visitDisplayName":"","orderOfDisplay":00}],"modalities":null,"examDates":null,"series":null,"transferType":null,"sftpLocations":[],"dicomLocations":[],"fileSystemLocations":[],"rawFileSystemLocations":[],"customFolder":null,"folderStructure":null,"customFile":null,"fileStructure":null,"includePS":null}'
def newUpdatedPayLoad = jsonConvert.parseText(updatedPayLoad);
def jsonBuilder = new JsonBuilder(newUpdatedPayLoad)
jsonBuilder.content.visits[0].visitSubmitName = visitSubmitNameR
jsonBuilder.content.visits[0].visitDisplayName = visitDisplayNameR
jsonBuilder.content.visits[0].orderOfDisplay = newORDEROFDIS
vars.put('finalPayLoad',jsonBuilder.toPrettyString())
}
else {
vars.put('finalPayLoad',defaultPayload)
}
Understand from many articles on stack overflow that the filter method in the tweepy.streaming.stream class uses a logical OR for track and location arguements
so the below will return either tweets from location=USA or with a word ""
streamObj = tweepy.streaming.Stream(oauthObject
,EchoStreamListener(api=apiInstance,
dump_json=args.json,
numtweets=args.numtweets))
keyWordList = ['panthers','falcon']
GEOBOX_USA = [-125,25.1,-60.5,49.1]
streamObj.filter(locations=GEOBOX_USA, track=keyWordList, languages=['en'])
This solution (How to add a location filter to tweepy module
) to check keywords in the on_status method works great, but if i needed to store the entire json variable i think i would have to use the on_data
so changed the on_data (as shown in code below), but get an error:
File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 294, in _run
raise exception
KeyError: 'text'
-- coding: utf-8 --
from types import *
import tweepy
import json
import argparse
import io
class EchoStreamListener(tweepy.StreamListener):
def __init__(self, api, dump_json=False, numtweets=0):
self.api = api
self.dump_json = dump_json
self.count = 0
self.limit = int(numtweets)
super(tweepy.StreamListener, self).__init__()
# def on_status(self, status):
# if any(keyWord in status.text.lower() for keyWord in keyWordList):
# print status.text
#
# self.count+=1
# return False if self.count == self.limit else True
# else:
# return True # Don't kill the stream
def on_data(self, tweet):
tweet_data = json.loads(tweet) # This allows the JSON data be used as a normal dictionary:
if any(keyWord in tweet_data['text'] for keyWord in keyWordList):
if self.dump_json:
print json.dumps(tweet_data)
saveFile.write(unicode(tweet) + "\n")
self.count+=1
return False if self.count == self.limit else True
else:
print tweet_data['created_at','name','text'].encode("utf-8").rstrip()
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def get_parser():
parser = argparse.ArgumentParser(add_help=True)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-j', '--json',
action='store_true',
help='dump each tweet as a json string'
)
group.add_argument(
'-t', '--text',
dest='json',
action='store_false',
help='dump each tweet\'s text'
)
parser.add_argument(
'-n', '--numtweets',
metavar='numtweets',
help='set number of tweets to retrieve'
)
return parser
if __name__ == '__main__':
oauthObject = tweepy.OAuthHandler(myconsumer_key, myconsumer_secret)
oauthObject.set_access_token(myaccess_key,myaccess_secret)
apiInstance = tweepy.API(oauthObject)
parser = get_parser()
args = parser.parse_args()
streamObj = tweepy.streaming.Stream(oauthObject
,EchoStreamListener(api=apiInstance,
dump_json=args.json,
numtweets=args.numtweets))
keyWordList = ['panthers','falcon']
GEOBOX_USA = [-125,25.1,-60.5,49.1]
saveFile = io.open('/Users/deepaktanna/raw_tweets.json', 'w', encoding='utf-8')
streamObj.filter(locations=GEOBOX_USA, languages=['en'])
saveFile.close()