nmraoI entered this (see screenshot), but it's not working. as you can see there is an accesstoken in the response, but validating it gives an error (it thinks it should be false?). Do you have any tips?
","body@stringLength":"1218","rawBody":"
wrote:
For the mentioned case, JsonPatMatch is fine.
$.accessToken should equal to the desired value.
","author":{"__ref":"User:user:69348"},"isEscalated":null,"postTime":"2017-04-03T08:03:02.052-07:00","solution":false,"customFields":[],"attachments":{"__typename":"AttachmentConnection","edges":[],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"repliesCount":5},"Revision:revision:139201_1":{"__typename":"Revision","id":"revision:139201_1","lastEditTime":"2017-04-03T10:14:09.468-07:00"},"QueryVariables:ReplyList:message:121682:2":{"__typename":"QueryVariables","id":"ReplyList:message:121682:2","value":{"id":"message:121682","first":10,"sorts":{"kudosSumWeight":{"direction":"DESC","order":0},"postTime":{"direction":"ASC","order":1}},"repliesFirst":3,"repliesFirstDepthThree":1,"repliesSorts":{"kudosSumWeight":{"direction":"DESC","order":0},"postTime":{"direction":"ASC","order":1}},"useAvatar":true,"useAuthorLogin":true,"useAuthorRank":true,"useBody":true,"useKudosCount":true,"useTimeToRead":false,"useMedia":false,"useReadOnlyIcon":false,"useRepliesCount":true,"useSearchSnippet":false,"useAcceptedSolutionButton":true,"useSolvedBadge":false,"useAttachments":false,"attachmentsFirst":5,"useTags":false,"useNodeAncestors":false,"useUserHoverCard":false,"useNodeHoverCard":false,"useModerationStatus":true,"usePreviewSubjectModal":false,"useMessageStatus":true}},"ROOT_MUTATION":{"__typename":"Mutation"},"CachedAsset:text:en_US-shared/client/components/users/UserAvatar-1749759380271":{"__typename":"CachedAsset","id":"text:en_US-shared/client/components/users/UserAvatar-1749759380271","value":{"altText":"{login}'s avatar","altTextGeneric":"User's avatar"},"localOverride":false},"CachedAsset:text:en_US-shared/client/components/ranks/UserRankLabel-1749759380271":{"__typename":"CachedAsset","id":"text:en_US-shared/client/components/ranks/UserRankLabel-1749759380271","value":{"altTitle":"Icon for {rankName} rank"},"localOverride":false},"CachedAsset:text:en_US-components/common/ExternalLinkWarningModal-1749759380271":{"__typename":"CachedAsset","id":"text:en_US-components/common/ExternalLinkWarningModal-1749759380271","value":{"title":"Leaving the Community","description":"You're about to leave this site and navigate to an external domain. Are you sure you want to continue?","action.submit":"Continue","action.cancel":"Go Back"},"localOverride":false},"CachedAsset:text:en_US-components/messages/AcceptedSolutionButton-1749759380271":{"__typename":"CachedAsset","id":"text:en_US-components/messages/AcceptedSolutionButton-1749759380271","value":{"accept":"Mark as Solution","accepted":"Marked as Solution","errorHeader":"Error!","errorAdd":"There was an error marking as solution.","errorRemove":"There was an error unmarking as solution.","solved":"Solved","topicAlreadySolvedErrorTitle":"Solution Already Exists","topicAlreadySolvedErrorDesc":"Refresh the browser to view the existing solution"},"localOverride":false},"CachedAsset:text:en_US-components/messages/ThreadedReplyList-1749759380271":{"__typename":"CachedAsset","id":"text:en_US-components/messages/ThreadedReplyList-1749759380271","value":{"title":"{count, plural, one{# Reply} other{# Replies}}","title@board:BLOG":"{count, plural, one{# Comment} other{# Comments}}","title@board:TKB":"{count, plural, one{# Comment} other{# Comments}}","title@board:IDEA":"{count, plural, one{# Comment} other{# Comments}}","title@board:OCCASION":"{count, plural, one{# Comment} other{# Comments}}","noRepliesTitle":"No Replies","noRepliesTitle@board:BLOG":"No Comments","noRepliesTitle@board:TKB":"No Comments","noRepliesTitle@board:IDEA":"No Comments","noRepliesTitle@board:OCCASION":"No Comments","noRepliesDescription":"Be the first to reply","noRepliesDescription@board:BLOG":"Be the first to comment","noRepliesDescription@board:TKB":"Be the first to comment","noRepliesDescription@board:IDEA":"Be the first to comment","noRepliesDescription@board:OCCASION":"Be the first to comment","messageReadOnlyAlert:BLOG":"Comments have been turned off for this post","messageReadOnlyAlert:TKB":"Comments have been turned off for this article","messageReadOnlyAlert:IDEA":"Comments have been turned off for this idea","messageReadOnlyAlert:FORUM":"Replies have been turned off for this discussion","messageReadOnlyAlert:OCCASION":"Comments have been turned off for this event"},"localOverride":false},"ForumReplyMessage:message:19451":{"__typename":"ForumReplyMessage","id":"message:19451","customFields":[],"entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417/message:19451","revisionNum":1,"attachments":{"__typename":"AttachmentConnection","edges":[],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"uid":19451,"author":{"__ref":"User:user:10568"},"readOnly":false,"repliesCount":6},"ForumReplyMessage:message:121682":{"__typename":"ForumReplyMessage","id":"message:121682","customFields":[],"entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417/message:121682","revisionNum":2,"attachments":{"__typename":"AttachmentConnection","edges":[],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"uid":121682,"author":{"__ref":"User:user:70896"},"readOnly":false,"repliesCount":5,"parent":{"__ref":"ForumReplyMessage:message:19451"},"replies":{"__typename":"MessageConnection","edges":[{"__typename":"MessageEdge","cursor":"MjUuNHwyLjF8b3wxMHwxNDowLDM5OjF8MQ","node":{"__ref":"AcceptedSolutionMessage:message:121687"}},{"__typename":"MessageEdge","cursor":"MjUuNHwyLjF8b3wxMHwxNDowLDM5OjF8Mg","node":{"__ref":"ForumReplyMessage:message:121685"}}],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}}},"User:user:10568":{"__typename":"User","id":"user:10568","entityType":"USER","eventPath":"community:nwkab66374/user:10568","deleted":false,"uid":10568,"login":"Holly_Greger","biography":null,"registrationData":{"__typename":"RegistrationData","status":null,"registrationTime":"2010-05-20T06:47:53.000-07:00"}},"Rank:rank:26":{"__typename":"Rank","id":"rank:26","position":9,"name":"Community Hero","color":"00537C","icon":null,"rankStyle":"OUTLINE"},"User:user:21201":{"__typename":"User","id":"user:21201","uid":21201,"login":"nmrao","biography":null,"registrationData":{"__typename":"RegistrationData","status":null,"registrationTime":"2013-05-17T20:34:18.000-07:00"},"deleted":false,"email":"","avatar":{"__typename":"UserAvatar","url":"https://community.smartbear.com/t5/s/nwkab66374/m_assets/avatars/default/avatar-5.svg?time=0"},"rank":{"__ref":"Rank:rank:26"},"entityType":"USER","eventPath":"community:nwkab66374/user:21201"},"User:user:70896":{"__typename":"User","id":"user:70896","entityType":"USER","eventPath":"community:nwkab66374/user:70896","deleted":false,"uid":70896,"login":"WillC","biography":null,"registrationData":{"__typename":"RegistrationData","status":null,"registrationTime":"2016-06-07T04:57:55.855-07:00"},"email":"","avatar":{"__typename":"UserAvatar","url":"https://community.smartbear.com/t5/s/nwkab66374/m_assets/avatars/default/avatar-3.svg?time=0"},"rank":{"__ref":"Rank:rank:14"}},"ForumTopicMessage:message:19417":{"__typename":"ForumTopicMessage","uid":19417,"subject":"How do i get JSON values from a response in Groovy","id":"message:19417","entityType":"FORUM_TOPIC","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417","revisionNum":1,"repliesCount":13},"Conversation:conversation:19417":{"__typename":"Conversation","id":"conversation:19417","solved":true,"topic":{"__ref":"ForumTopicMessage:message:19417"},"lastPostingActivityTime":"2016-06-20T19:53:22.952-07:00","lastPostTime":"2016-06-20T19:53:22.952-07:00","unreadReplyCount":13,"isSubscribed":false},"ModerationData:moderation_data:121687":{"__typename":"ModerationData","id":"moderation_data:121687","status":"APPROVED","rejectReason":null,"isReportedAbuse":false,"rejectUser":null,"rejectTime":null,"rejectActorType":null},"AcceptedSolutionMessage:message:121687":{"__typename":"AcceptedSolutionMessage","uid":121687,"id":"message:121687","entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417/message:121687","revisionNum":2,"author":{"__ref":"User:user:21201"},"readOnly":false,"repliesCount":3,"depth":3,"hasGivenKudo":false,"subscribed":false,"board":{"__ref":"Forum:board:SoapUI_OS"},"parent":{"__ref":"ForumReplyMessage:message:121682"},"conversation":{"__ref":"Conversation:conversation:19417"},"subject":"Re: How do i get JSON values from a response in Groovy","moderationData":{"__ref":"ModerationData:moderation_data:121687"},"body":"
Here is the later part of the script:
\n\n
It was not working because, items is a Json Array.
\n\n
json.items.eachWithIndex{ item, index ->\n log.info \"Item #: ${index}\"\n log.info \"Item resource Id: ${tem.resourceId}\"\n log.info \"Item name : ${item.name}\"\n log.info \"Item resource type : ${item.resourceType}\"\n log.info \"Item status : ${item.status}\"","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"216","kudosSumWeight":2,"postTime":"2016-06-20T17:44:46.046-07:00","lastPublishTime":"2016-06-20T19:52:05.224-07:00","solution":true,"metrics":{"__typename":"MessageMetrics","views":32611},"visibilityScope":"PUBLIC","placeholder":false,"showMoveIndicator":false,"originalMessageForPlaceholder":null,"isEscalated":null,"replies":{"__typename":"MessageConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[{"__typename":"MessageEdge","cursor":"MjUuNHwyLjF8b3wzfDE0OjAsMzk6MXwx","node":{"__ref":"ForumReplyMessage:message:121688"}}]}},"ModerationData:moderation_data:121688":{"__typename":"ModerationData","id":"moderation_data:121688","status":"APPROVED","rejectReason":null,"isReportedAbuse":false,"rejectUser":null,"rejectTime":null,"rejectActorType":null},"ForumReplyMessage:message:121688":{"__typename":"ForumReplyMessage","uid":121688,"id":"message:121688","entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417/message:121688","revisionNum":1,"author":{"__ref":"User:user:70896"},"readOnly":false,"repliesCount":2,"depth":4,"hasGivenKudo":false,"subscribed":false,"board":{"__ref":"Forum:board:SoapUI_OS"},"parent":{"__ref":"AcceptedSolutionMessage:message:121687"},"conversation":{"__ref":"Conversation:conversation:19417"},"subject":"Re: How do i get JSON values from a response in Groovy","moderationData":{"__ref":"ModerationData:moderation_data:121688"},"body":"
}
Thank you Rao. That makes perfect sense. This withEachIndex is exactly what I was looking for. I suspect this will help others as well. I am still throwing an error after many editing attempts. I've done some searching...not sure what is missing with a find or subsequent step. Thank you, again.
I) Current JSON Parser
package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//define the location of the JSON file
def response = context.expand( '${get_descendants#Response}' ).toString()
//log.info(response)
//define \"json\" which will be the name of my variable
def json = new JsonSlurper().parseText (response)
json.items.eachWithIndex
{
item, index ->
log.inf \"totalResults: ${index}\"
log.inf \"Item resourceId: ${item.resourceId}\"
log.info \"Item name: ${item.name}\"
log.info \"Item resource type: ${item.resourceType}\"
log.info \"Item status: ${item.status}\"
}
II) Error Message thrown (if I comment out totalResults line same error occurs for subsequent lines):
groovy.lang.MissingMethodException:
No signature of method: org.apache.log4j.Logger.inf() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [totalResults: 0]
Possible solutions: info(java.lang.Object), find(), any(), info(java.lang.Object, java.lang.Throwable), is(java.lang.Object), find(groovy.lang.Closure)
","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"228","kudosSumWeight":0,"postTime":"2016-06-20T18:29:37.651-07:00","lastPublishTime":"2016-06-20T18:29:37.651-07:00","metrics":{"__typename":"MessageMetrics","views":12477},"visibilityScope":"PUBLIC","placeholder":false,"showMoveIndicator":false,"originalMessageForPlaceholder":null,"isEscalated":null,"solution":false,"replies":{"__typename":"MessageConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[{"__typename":"MessageEdge","cursor":"MjUuNHwyLjF8b3wxfDE0OjAsMzk6MXwx","node":{"__ref":"ForumReplyMessage:message:121689"}}]}},"ModerationData:moderation_data:121689":{"__typename":"ModerationData","id":"moderation_data:121689","status":"APPROVED","rejectReason":null,"isReportedAbuse":false,"rejectUser":null,"rejectTime":null,"rejectActorType":null},"ForumReplyMessage:message:121689":{"__typename":"ForumReplyMessage","author":{"__ref":"User:user:70896"},"id":"message:121689","entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:19417/message:121689","revisionNum":1,"uid":121689,"depth":5,"hasGivenKudo":false,"subscribed":false,"board":{"__ref":"Forum:board:SoapUI_OS"},"parent":{"__ref":"ForumReplyMessage:message:121688"},"conversation":{"__ref":"Conversation:conversation:19417"},"subject":"Re: How do i get JSON values from a response in Groovy","moderationData":{"__ref":"ModerationData:moderation_data:121689"},"body":"Problem resolved. Typo only. Below is the working indexed code for future reference:
package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*
//define the location of the JSON file
def response = context.expand( '${get_descendants#Response}' ).toString()
//log.info(response)
//define \"json\" which will be the name of my variable
def json = new JsonSlurper().parseText (response)
json.items.eachWithIndex
{
item, index ->
log.info \"totalResults: ${index}\"
log.info \"Item resourceId: ${item.resourceId}\"
log.info \"Item name: ${item.name}\"
log.info \"Item resource type: ${item.resourceType}\"
log.info \"Item status: ${item.status}\"
}
Hi,
HKosova as you suggested, I implemented the JsonPath Match,
the strange this is, that validation initially succeeds (I clicked on \"select from current\"), however when re-executing the test it fails, and the reason for that is that expected value is changed, a comma is added to the end (see second screenshot).
After re-selecting the value again (\" select from current\"), the assertion passes again first time, but re-executing the test, makes the assertion fail again (comma is added again). Is this known behaviour? Possibly a bug?
","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"218","kudosSumWeight":0,"postTime":"2017-04-04T06:14:33.884-07:00","lastPublishTime":"2017-04-04T06:14:33.884-07:00","metrics":{"__typename":"MessageMetrics","views":12363},"visibilityScope":"PUBLIC","placeholder":false,"showMoveIndicator":false,"originalMessageForPlaceholder":null,"isEscalated":null,"solution":false,"replies":{"__typename":"MessageConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[{"__typename":"MessageEdge","cursor":"MjUuNHwyLjF8b3wzfDE0OjAsMzk6MXwx","node":{"__ref":"ForumReplyMessage:message:139565"}}]},"customFields":[],"attachments":{"__typename":"AttachmentConnection","edges":[],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}}},"ModerationData:moderation_data:139565":{"__typename":"ModerationData","id":"moderation_data:139565","status":"APPROVED","rejectReason":null,"isReportedAbuse":false,"rejectUser":null,"rejectTime":null,"rejectActorType":null},"ForumReplyMessage:message:139565":{"__typename":"ForumReplyMessage","author":{"__ref":"User:user:12406"},"id":"message:139565","entityType":"FORUM_REPLY","eventPath":"category:soapui-os-community/community:nwkab66374board:SoapUI_OS/message:139172/message:139565","revisionNum":1,"uid":139565,"depth":5,"hasGivenKudo":false,"subscribed":false,"board":{"__ref":"Forum:board:SoapUI_OS"},"parent":{"__ref":"ForumReplyMessage:message:139244"},"conversation":{"__ref":"Conversation:conversation:139172"},"subject":"Re: How to assert on value in a Json response?","moderationData":{"__ref":"ModerationData:moderation_data:139565"},"body":"
Mathijs,
\n\n
The comma is part of the error message, not the value. And the error happens because the token value is different - e.g. on your image the expected value ends with \"...D8LE\" and the actual value ends with \"... JSPPA\" (or vice versa).
\n\n
I guess the token is generated dynamically and is different for each new session, like a unique session ID. How are you supposed to validate it? Do you just need to verify this token is present in the response, regardless of the value?
","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"208","kudosSumWeight":0,"repliesCount":0,"postTime":"2017-04-10T00:38:46.598-07:00","lastPublishTime":"2017-04-10T00:38:46.598-07:00","metrics":{"__typename":"MessageMetrics","views":12301},"visibilityScope":"PUBLIC","placeholder":false,"showMoveIndicator":false,"originalMessageForPlaceholder":null,"isEscalated":null,"solution":false,"replies":{"__typename":"MessageConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[]},"customFields":[],"attachments":{"__typename":"AttachmentConnection","edges":[],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}}}}}},"page":"/forums/ForumMessagePage/ForumMessagePage","query":{"boardId":"soapui_os","messageSubject":"how-to-assert-on-value-in-a-json-response","messageId":"139172","replyId":"139201"},"buildId":"3XH0qYWYCnEYycuN5W4S8","runtimeConfig":{"buildInformationVisible":false,"logLevelApp":"info","logLevelMetrics":"info","surveysEnabled":true,"openTelemetry":{"clientEnabled":false,"configName":"smartbear","serviceVersion":"25.4.0","universe":"prod","collector":"http://localhost:4318","logLevel":"error","routeChangeAllowedTime":"5000","headers":"","enableDiagnostic":"false","maxAttributeValueLength":"4095"},"apolloDevToolsEnabled":false,"quiltLazyLoadThreshold":"3"},"isFallback":false,"isExperimentalCompile":false,"dynamicIds":["components_seo_QAPageSchema","components_community_Navbar_NavbarWidget","components_community_Breadcrumb_BreadcrumbWidget","components_messages_TopicWithThreadedReplyListWidget","components_messages_MessageListForNodeByRecentActivityWidget","components_messages_RelatedContentWidget","components_customComponent_CustomComponent","components_messages_MessageView_MessageViewStandard","components_messages_EscalatedMessageBanner","shared_client_components_common_List_UnstyledList","components_messages_MessageView","components_messages_MessageView_MessageViewInline","shared_client_components_common_Pager_PagerLoadMore","components_customComponent_CustomComponentContent_TemplateContent"],"appGip":true,"scriptLoader":[]}