Forum Discussion

Anket_Chaudhary's avatar
Anket_Chaudhary
New Member
11 months ago

SoapUI JSON response DateTime format validation

I wanted to validate only format of the JSON response eg my response is "StatusDateTime":"01-01-2001 09:02:58".

Now I want to put assertion who will validate that this datetime is coming in proper format or not.
  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    There's loads of examples on the Internet about using Groovy to validate a string against a given date format.  Lot's of regular expression examples and so on.  I've done this elsewhere, but I use Date Parse inside a try-catch-finally block.

     

    E.g. Here's Groovy snippet that shows how the date does not parse...

    import static groovy.test.GroovyAssert.assertTrue;
    def validated;
    
    try {
    	validated = false;
    	def invalidDate = Date.parse('dd-MM-yyyy HH:mm:ss', '01\01\2001 09:02:58');
    	validated = true;
    } catch(java.text.ParseException ex) {
    	log.info("Cannot Parse " + ex);
    	validated = false;
    } finally {
    	log.info("Is my date string valid? " + validated);	
    	//assertTrue("Is my date string valid? ", validated);
    	return validated;
    }

     

    And here's an example where it does parse and is therefore valid.

    import static groovy.test.GroovyAssert.assertTrue;
    def validated;
    
    try {
    	validated = false;
    	def validDate = Date.parse('dd-MM-yyyy HH:mm:ss', '01-01-2001 09:02:58');
    	validated = true;
    } catch(java.text.ParseException ex) {
    	log.info("Cannot Parse " + ex);
    	validated = false;
    } finally {
    	log.info("Is my date string valid? " + validated);	
    	//assertTrue("Is my date string valid? ", validated);
    	return validated;
    }

    This example works as a Groovy script.  But if you dropped this into a script assertion, you can then use the commented out assertTrue as a real assertion.

     

  • To validate the DateTime format in your JSON response using SoapUI, you can utilize a script assertion. Write a script that checks the format of "StatusDateTime" against your expected format ("dd-MM-yyyy HH:mm:ss"). This way, you can ensure the DateTime is in the proper format. If you need assistance with the script, feel free to ask reviews!   #SoapUI #JSONValidation