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" : "user@gmail.com", "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,

WEshruth
  • 769
  • 3
  • 16
  • 32
  • 1
    You can *try* parsing the json, catching the exception if it is not valid. – Rudolf Mühlbauer Oct 15 '12 at 07:02
  • @RudolfMühlbauer: Please add comments as answers, if they are answers and not comments. – Alex Reynolds Oct 15 '12 at 07:03
  • 1
    @AlexReynolds It seems to me that Rudolf's is a suggestion to try something *before asking* more than a solution... – Gabber Oct 15 '12 at 07: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, ... – Rudolf Mühlbauer Oct 15 '12 at 07: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. – Rudolf Mühlbauer Oct 15 '12 at 07:18
  • @Vishruth, sorry for the rant ;) - no pun intended! – Rudolf Mühlbauer Oct 15 '12 at 07:23

2 Answers2

8
import json
try:
  json.loads(data)
except ValueError:
  print("data was not valid JSON")
Deestan
  • 16,738
  • 4
  • 32
  • 48
  • This. JSON is not regular, and Python regexes are therefore fundamentally unable to validate JSON. – Tim Pietzcker Oct 15 '12 at 09:34
  • @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 Oct 15 '12 at 09:45
  • @Tim Pietzcker: Sorry!, Ya its working fine. i was doing in other way! – WEshruth Oct 15 '12 at 12:22
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

Community
  • 1
  • 1
Gabber
  • 5,152
  • 6
  • 35
  • 49