1

This is my test class and i am trying to unittest my method that is createaccount()

class CreateAccountTest1(unittest.TestCase):
    def testCreateAccount_1(self,data):
        text = "{'user_id':'abc123','action':'add','names':['hello','world']}"
        regex = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|   [^"\\])*"/ g, ''))) && eval('(' + text + ')')
        self.assertRegexpMatches(text, reg, 'my msg')

createaccount() method is

class CreateAccountClass():
    def CreateAccount(self,data):

Now i have to check whether the parameter of the createaccount() is in json format or not.

if i pass data=

 { "_id" : "[email protected]", "H_id" : "smsg0", "name" : "vish", "passwrd" : "xj45cd" }

it should check whether it is json or not, and i am sure that this is in json format. now in my method createaccount() it should check whether the data is in json format or not, if not it should print error message, if it works with regex ? or any suggestions, Thanks,

6
  • 1
    You can try parsing the json, catching the exception if it is not valid. Commented Oct 15, 2012 at 7:02
  • @RudolfMühlbauer: Please add comments as answers, if they are answers and not comments. Commented Oct 15, 2012 at 7:03
  • 1
    @AlexReynolds It seems to me that Rudolf's is a suggestion to try something before asking more than a solution... Commented Oct 15, 2012 at 7:08
  • @AlexReynolds, you are right. But, depending on my mood, I miss a lot of RTFM / WhatHaveYouTried here in SO. So i use comments to give hints, instead of posting 4 lines of code, which can easily be found on the web, a book, a tutorial, ... Commented Oct 15, 2012 at 7:09
  • The thing is that when such a question is posted, 10 people start up google or the python shell to test their answer. That consumes more time and has learning effect for the OP than if the OP would try to figure it out himself. Commented Oct 15, 2012 at 7:18

2 Answers 2

9
import json
try:
  json.loads(data)
except ValueError:
  print("data was not valid JSON")
Sign up to request clarification or add additional context in comments.

3 Comments

This. JSON is not regular, and Python regexes are therefore fundamentally unable to validate JSON.
@Vishruth: What do you mean? This code works correctly in validating whether a given string data contains valid JSON. Whether it matches the data format you're expecting is a different question and has nothing to do with JSON validation per se. My comment above meant to say that the regex approach is futile and that you need to use a parser instead.
@Tim Pietzcker: Sorry!, Ya its working fine. i was doing in other way!
1

Look at this answer. Also, I'd suggest not to perform this check with regexes, just do it with a standard parser and check for the errors using json.load

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.