Validate JSON String using REGEX in JSONAssert java
I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings.
"timestamp": "^\d4-\d2-\d2T\d2:\d2:\d2.\d3+\d4$",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=ISO-8859-1' not supported",
"path": "/service/addUser"
My actual response consists of timestamp in this format 2018-11-13T04:10:11.233+0000
JSONAssert.assertEquals(getJsonBody(expected), response.asString(),false);
Is always giving the below error on the regular expression
java.lang.AssertionError: timestamp
Expected: ^d4-d2-d2Td2:d2:d2.d3+d4$
got: 2018-11-13T04:12:55.923+0000
Any recommendation on this error?
java json jsonassert
|
show 2 more comments
I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings.
"timestamp": "^\d4-\d2-\d2T\d2:\d2:\d2.\d3+\d4$",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=ISO-8859-1' not supported",
"path": "/service/addUser"
My actual response consists of timestamp in this format 2018-11-13T04:10:11.233+0000
JSONAssert.assertEquals(getJsonBody(expected), response.asString(),false);
Is always giving the below error on the regular expression
java.lang.AssertionError: timestamp
Expected: ^d4-d2-d2Td2:d2:d2.d3+d4$
got: 2018-11-13T04:12:55.923+0000
Any recommendation on this error?
java json jsonassert
instead of comparing values, you should do something likeassertTrue(response.asString().matches(expected))
– Kartik
Nov 13 '18 at 4:19
1
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try^d4-d2-d2Td2:d2:d2.d3+d4$
– D.B.
Nov 13 '18 at 4:23
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I have added this regex[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4but didnt worked
– vkrams
Nov 13 '18 at 4:53
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57
|
show 2 more comments
I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings.
"timestamp": "^\d4-\d2-\d2T\d2:\d2:\d2.\d3+\d4$",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=ISO-8859-1' not supported",
"path": "/service/addUser"
My actual response consists of timestamp in this format 2018-11-13T04:10:11.233+0000
JSONAssert.assertEquals(getJsonBody(expected), response.asString(),false);
Is always giving the below error on the regular expression
java.lang.AssertionError: timestamp
Expected: ^d4-d2-d2Td2:d2:d2.d3+d4$
got: 2018-11-13T04:12:55.923+0000
Any recommendation on this error?
java json jsonassert
I am storing my expected json string in the json file under resources as shown below. The json string consists of regular expression. I am using JSONAssert Library to compare two json strings.
"timestamp": "^\d4-\d2-\d2T\d2:\d2:\d2.\d3+\d4$",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=ISO-8859-1' not supported",
"path": "/service/addUser"
My actual response consists of timestamp in this format 2018-11-13T04:10:11.233+0000
JSONAssert.assertEquals(getJsonBody(expected), response.asString(),false);
Is always giving the below error on the regular expression
java.lang.AssertionError: timestamp
Expected: ^d4-d2-d2Td2:d2:d2.d3+d4$
got: 2018-11-13T04:12:55.923+0000
Any recommendation on this error?
java json jsonassert
java json jsonassert
edited Nov 13 '18 at 4:20
vkrams
asked Nov 13 '18 at 4:15
vkramsvkrams
3,194145189
3,194145189
instead of comparing values, you should do something likeassertTrue(response.asString().matches(expected))
– Kartik
Nov 13 '18 at 4:19
1
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try^d4-d2-d2Td2:d2:d2.d3+d4$
– D.B.
Nov 13 '18 at 4:23
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I have added this regex[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4but didnt worked
– vkrams
Nov 13 '18 at 4:53
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57
|
show 2 more comments
instead of comparing values, you should do something likeassertTrue(response.asString().matches(expected))
– Kartik
Nov 13 '18 at 4:19
1
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try^d4-d2-d2Td2:d2:d2.d3+d4$
– D.B.
Nov 13 '18 at 4:23
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I have added this regex[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4but didnt worked
– vkrams
Nov 13 '18 at 4:53
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57
instead of comparing values, you should do something like
assertTrue(response.asString().matches(expected))– Kartik
Nov 13 '18 at 4:19
instead of comparing values, you should do something like
assertTrue(response.asString().matches(expected))– Kartik
Nov 13 '18 at 4:19
1
1
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try
^d4-d2-d2Td2:d2:d2.d3+d4$– D.B.
Nov 13 '18 at 4:23
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try
^d4-d2-d2Td2:d2:d2.d3+d4$– D.B.
Nov 13 '18 at 4:23
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I have added this regex
[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4 but didnt worked– vkrams
Nov 13 '18 at 4:53
I have added this regex
[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4 but didnt worked– vkrams
Nov 13 '18 at 4:53
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57
|
show 2 more comments
1 Answer
1
active
oldest
votes
You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.
Try this code:-
String expected = "n" +
" "timestamp": "^\\d4-\\d2-\\d2T\\d2:\\d2:\\d2\\.\\d3\\+\\d4$",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
String actual = "n" +
" "timestamp": "2018-11-13T04:12:55.923+0000",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
JSONAssert.assertEquals(
expected,
actual,
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
So with your code, it will look something like:-
JSONAssert.assertEquals(
getJsonBody(expected),
response.asString(),
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273716%2fvalidate-json-string-using-regex-in-jsonassert-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.
Try this code:-
String expected = "n" +
" "timestamp": "^\\d4-\\d2-\\d2T\\d2:\\d2:\\d2\\.\\d3\\+\\d4$",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
String actual = "n" +
" "timestamp": "2018-11-13T04:12:55.923+0000",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
JSONAssert.assertEquals(
expected,
actual,
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
So with your code, it will look something like:-
JSONAssert.assertEquals(
getJsonBody(expected),
response.asString(),
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
add a comment |
You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.
Try this code:-
String expected = "n" +
" "timestamp": "^\\d4-\\d2-\\d2T\\d2:\\d2:\\d2\\.\\d3\\+\\d4$",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
String actual = "n" +
" "timestamp": "2018-11-13T04:12:55.923+0000",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
JSONAssert.assertEquals(
expected,
actual,
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
So with your code, it will look something like:-
JSONAssert.assertEquals(
getJsonBody(expected),
response.asString(),
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
add a comment |
You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.
Try this code:-
String expected = "n" +
" "timestamp": "^\\d4-\\d2-\\d2T\\d2:\\d2:\\d2\\.\\d3\\+\\d4$",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
String actual = "n" +
" "timestamp": "2018-11-13T04:12:55.923+0000",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
JSONAssert.assertEquals(
expected,
actual,
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
So with your code, it will look something like:-
JSONAssert.assertEquals(
getJsonBody(expected),
response.asString(),
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
You are comparing the pattern with the timestamp string. What you actually need to do is check if the timestamp matches the pattern.
Try this code:-
String expected = "n" +
" "timestamp": "^\\d4-\\d2-\\d2T\\d2:\\d2:\\d2\\.\\d3\\+\\d4$",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
String actual = "n" +
" "timestamp": "2018-11-13T04:12:55.923+0000",n" +
" "status": 415,n" +
" "error": "Unsupported Media Type",n" +
" "message": "Content type 'text/plain;charset=ISO-8859-1' not supported",n" +
" "path": "/service/addUser"n" +
"";
JSONAssert.assertEquals(
expected,
actual,
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
So with your code, it will look something like:-
JSONAssert.assertEquals(
getJsonBody(expected),
response.asString(),
new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("***", new RegularExpressionValueMatcher<>())
)
);
edited Nov 13 '18 at 5:08
answered Nov 13 '18 at 4:40
KartikKartik
3,19931435
3,19931435
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
add a comment |
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
That works :-) Thank you
– vkrams
Nov 13 '18 at 5:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273716%2fvalidate-json-string-using-regex-in-jsonassert-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
instead of comparing values, you should do something like
assertTrue(response.asString().matches(expected))– Kartik
Nov 13 '18 at 4:19
1
Your regular expression is incorrect - dot and plus have special meaning and must be escaped. Try
^d4-d2-d2Td2:d2:d2.d3+d4$– D.B.
Nov 13 '18 at 4:23
I did escaped but it doesn't work. Same error
– vkrams
Nov 13 '18 at 4:42
I have added this regex
[0-9]4-[0-9]2-[0-9]2T[0-9]2:[0-9]2:[0-9]2\.[0-9]3\+[0-9]4but didnt worked– vkrams
Nov 13 '18 at 4:53
@Kartik I got some pattern matcher exception. Let me fix that
– vkrams
Nov 13 '18 at 4:57