{
\"Name\" : \"Name1\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"453\"
},
{
\"Name\" : \"Name2\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"424\"
}
JSON2
{
\"Name\" : \"Name3\",
\"Age\" : \"21\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"424\"
},
{
\"Name\" : \"Name4\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"412\"
}
Hi,
I have two JSON responses that will have dynamic nodes on each request, but same structure and properties. I need to identify if a given property has no duplicate values found in both the JSON responses via groovy. Appreciate you help. Thanks
Example below. ID-424 in JSON1 & 2 are matching.
JSON1
{
\"Name\" : \"Name1\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"453\"
},
{
\"Name\" : \"Name2\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"424\"
}
JSON2
{
\"Name\" : \"Name3\",
\"Age\" : \"21\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"424\"
},
{
\"Name\" : \"Name4\",
\"Age\" : \"25\",
\"DateOfBirth\" : \"01012000\",
\"ID\" : \"412\"
}
Thanks nmrao I have now attached two JSON files that would provide some idea for the structure. In these two JSON files I'm looking for duplicate values for property - \"ReferenceNumber\". Ideally these JSON responses Im thinking will not be saved as files to be compared, but any way we can use groovy to read the responses and find the duplicate values for property \"ReferenceNumber\"
","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"203","kudosSumWeight":0,"postTime":"2021-06-29T18:14:23.035-07:00","lastPublishTime":"2021-06-29T18:14:23.035-07:00","metrics":{"__typename":"MessageMetrics","views":3370},"visibilityScope":"PUBLIC","placeholder":false,"originalMessageForPlaceholder":null,"isEscalated":null,"solution":false,"entityType":"FORUM_REPLY","eventPath":"category:ready_api_forums/community:nwkab66374board:readyapi-questions/message:219338/message:219344","replies":{"__typename":"MessageConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[{"__typename":"MessageEdge","cursor":"MjUuMnwyLjF8b3wxfDE0OjAsMzk6MXwx","node":{"__ref":"AcceptedSolutionMessage:message:219353"}}]},"customFields":[],"attachments":{"__typename":"AttachmentConnection","edges":[{"__typename":"AttachmentEdge","node":{"__ref":"Attachment:{\"id\":\"attachment:message219344AttachmentNumber1\",\"url\":\"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/1/JSON11.txt\"}"}},{"__typename":"AttachmentEdge","node":{"__ref":"Attachment:{\"id\":\"attachment:message219344AttachmentNumber2\",\"url\":\"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/2/JSON22.txt\"}"}}],"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"editFrozen":false,"body@stringLength":"575","rawBody":"Thanks
- add a script assertion to both the request test steps with the below code
\n- below script would check the response and makes the test fail if there are duplicate values for \"ReferenceNumber\" property
\n\n
Hoping this is what you need, reply back otherwise.
\n\n
def duplicateRefs = new groovy.json.JsonSlurper().parseText(context.response).ReferenceNumber.countBy{it}.findResults { it.value > 1 ? it.key : null }\nlog.info \"${duplicateRefs.size() > 0 ? 'Duplicate reference numbers found :' + duplicateRefs : 'No duplicate reference numbers'}\"\n\nassert [] == duplicateRefs, \"Duplicate references found, ${duplicateRefs}\"
","body@stripHtml({\"removeProcessingText\":false,\"removeSpoilerMarkup\":false,\"removeTocMarkup\":false,\"truncateLength\":200})@stringLength":"208","kudosSumWeight":1,"repliesCount":12,"postTime":"2021-06-30T00:31:00.996-07:00","lastPublishTime":"2021-06-30T00:31:00.996-07:00","solution":true,"metrics":{"__typename":"MessageMetrics","views":3461},"visibilityScope":"PUBLIC","placeholder":false,"originalMessageForPlaceholder":null,"isEscalated":null,"entityType":"FORUM_REPLY","eventPath":"category:ready_api_forums/community:nwkab66374board:readyapi-questions/message:219338/message:219353","customFields":[],"readOnly":false,"editFrozen":false,"body@stringLength":"928","rawBody":"- add a script assertion to both the request test steps with the below code
\n- below script would check the response and makes the test fail if there are duplicate values for \"ReferenceNumber\" property
\n\n
Hoping this is what you need, reply back otherwise.
\n\n
Here is the Groovy Script
\n- Add a groovy script after the second test request step
\n- Provide correct values step1, and step2
\n\n
\n
\n
//Update the Request step names below\ndef step1 = 'Name of the Request step1'\ndef step2 = 'Name of the Request step2'\n\n//Closure to get the ReferenceNumber from a json of the Given test step\ndef refNumbers = { step -> new groovy.json.JsonSlurper().parseText(context.testCase.testSteps[step].getPropertyValue('response')).ReferenceNumber }\ndef list1 = refNumbers(step1)\ndef list2 = refNumbers(step2)\nlog.info \"Reference Numbers for ${step1} are => ${list1}\"\nlog.info \"Reference Numbers for ${step2} are => ${list2}\"\nassert list1.every { !list2.contains(it) }, 'Does not match the expected result'
\n\n
\n
-
","body@stripHtml({\"removeProcessingText\":true,\"removeSpoilerMarkup\":true,\"removeTocMarkup\":true,\"truncateLength\":200})@stringLength":"228","postTime":"2021-06-30T08:07:31.971-07:00","lastPublishTime":"2021-06-30T09:28:45.812-07:00","images":{"__typename":"AssociatedImageConnection","edges":[],"totalCount":0,"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"attachments":{"__typename":"AttachmentConnection","pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null},"edges":[]},"solution":true,"metrics":{"__typename":"MessageMetrics","views":2299},"placeholder":false,"originalMessageForPlaceholder":null,"videos":{"__typename":"VideoConnection","edges":[],"totalCount":0,"pageInfo":{"__typename":"PageInfo","hasNextPage":false,"endCursor":null,"hasPreviousPage":false,"startCursor":null}},"isEscalated":null,"entityType":"FORUM_REPLY","eventPath":"category:ready_api_forums/community:nwkab66374board:readyapi-questions/message:219338/message:219392","customFields":[],"readOnly":false,"editFrozen":false,"body@stringLength":"1114","rawBody":"Here is the Groovy Script
\n- Add a groovy script after the second test request step
\n- Provide correct values step1, and step2
\n\n
\n
\n
\n
\n
-
","kudosSumWeight":1,"repliesCount":5,"timeToRead":1,"currentRevision":{"__ref":"Revision:revision:219392_3"},"latestVersion":null,"visibilityScope":"PUBLIC","messagePolicies":{"__typename":"MessagePolicies","canModerateSpamMessage":{"__typename":"PolicyResult","failureReason":{"__typename":"FailureReason","message":"error.lithium.policies.feature.moderation_spam.action.moderate_entity.allowed.accessDenied","key":"error.lithium.policies.feature.moderation_spam.action.moderate_entity.allowed.accessDenied","args":[]}}}},"Attachment:{\"id\":\"attachment:message219344AttachmentNumber1\",\"url\":\"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/1/JSON11.txt\"}":{"__typename":"Attachment","id":"attachment:message219344AttachmentNumber1","filename":"JSON11.txt","filesize":1780,"contentType":"text/plain","url":"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/1/JSON11.txt"},"Attachment:{\"id\":\"attachment:message219344AttachmentNumber2\",\"url\":\"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/2/JSON22.txt\"}":{"__typename":"Attachment","id":"attachment:message219344AttachmentNumber2","filename":"JSON22.txt","filesize":1972,"contentType":"text/plain","url":"https://community.smartbear.com/t5/s/nwkab66374/attachments/nwkab66374/readyapi-questions/53037/2/JSON22.txt"},"Revision:revision:219344_1":{"__typename":"Revision","id":"revision:219344_1","lastEditTime":"2021-06-29T18:14:23.035-07:00"},"CachedAsset:text:en_US-shared/client/components/users/UserAvatar-1741125846000":{"__typename":"CachedAsset","id":"text:en_US-shared/client/components/users/UserAvatar-1741125846000","value":{"altText":"{login}'s avatar","altTextGeneric":"User's avatar"},"localOverride":false},"CachedAsset:text:en_US-shared/client/components/ranks/UserRankLabel-1741125846000":{"__typename":"CachedAsset","id":"text:en_US-shared/client/components/ranks/UserRankLabel-1741125846000","value":{"altTitle":"Icon for {rankName} rank"},"localOverride":false},"CachedAsset:text:en_US-components/attachments/AttachmentView/AttachmentViewChip-1741125846000":{"__typename":"CachedAsset","id":"text:en_US-components/attachments/AttachmentView/AttachmentViewChip-1741125846000","value":{"errorTitle":"Failed!","previewFile":"Preview File","downloadFile":"Download File {name}","removeFile":"Remove File {name}","errorBadExtension":"This file does not have a valid extension. \"{extensions}\" are the valid extensions.","errorFileEmpty":"This file is empty or does not exist.","errorTooLarge":"The maximum file size is: {maxFileSize}.","errorTooMany":"Too many attachments. The maximum number of attachments per message is: {maxAttachmentCount, number, integer}.","errorDuplicate":"This file is already attached."},"localOverride":false},"Revision:revision:219392_3":{"__typename":"Revision","id":"revision:219392_3","lastEditTime":"2021-06-30T09:28:45.812-07:00"},"CachedAsset:text:en_US-components/tags/TagView/TagViewChip-1741125846000":{"__typename":"CachedAsset","id":"text:en_US-components/tags/TagView/TagViewChip-1741125846000","value":{"tagLabelName":"Tag name {tagName}"},"localOverride":false}}}},"page":"/forums/ForumMessagePage/ForumMessagePage","query":{"boardId":"readyapi-questions","messageSubject":"compare-2-json-responses-for-duplicate-property-value","messageId":"219338"},"buildId":"q_bLpq2mflH0BeZigxpj6","runtimeConfig":{"buildInformationVisible":false,"logLevelApp":"info","logLevelMetrics":"info","openTelemetryClientEnabled":false,"openTelemetryConfigName":"smartbear","openTelemetryServiceVersion":"25.2.0","openTelemetryUniverse":"prod","openTelemetryCollector":"http://localhost:4318","openTelemetryRouteChangeAllowedTime":"5000","apolloDevToolsEnabled":false,"inboxMuteWipFeatureEnabled":false},"isFallback":false,"isExperimentalCompile":false,"dynamicIds":["./components/seo/QAPageSchema/QAPageSchema.tsx","./components/community/Navbar/NavbarWidget.tsx","./components/community/Breadcrumb/BreadcrumbWidget.tsx","./components/messages/TopicWithThreadedReplyListWidget/TopicWithThreadedReplyListWidget.tsx","./components/messages/MessageListForNodeByRecentActivityWidget/MessageListForNodeByRecentActivityWidget.tsx","./components/messages/RelatedContentWidget/RelatedContentWidget.tsx","./components/customComponent/CustomComponent/CustomComponent.tsx","./components/messages/MessageView/MessageViewStandard/MessageViewStandard.tsx","./components/messages/ThreadedReplyList/ThreadedReplyList.tsx","../shared/client/components/common/List/UnstyledList/UnstyledList.tsx","./components/messages/MessageView/MessageView.tsx","../shared/client/components/common/Pager/PagerLoadMore/PagerLoadMore.tsx","./components/messages/MessageView/MessageViewInline/MessageViewInline.tsx","./components/customComponent/CustomComponentContent/TemplateContent.tsx","../shared/client/components/common/List/UnwrappedList/UnwrappedList.tsx","./components/attachments/AttachmentView/AttachmentView.tsx","./components/attachments/AttachmentView/AttachmentViewChip/AttachmentViewChip.tsx","./components/tags/TagView/TagView.tsx","./components/tags/TagView/TagViewChip/TagViewChip.tsx"],"appGip":true,"scriptLoader":[]}