I am trying to implement python Json parser with the following set of rules:
Rule 1:
A JSON data object always begins with curly braces and ends with curly braces. {}
Rule 2:
All data is represented in the form of
"string":value
where the value can be any of the following:
- number
- string
- boolean
- another json
Rule 3:
The rules for forming the strings (on the left hand side in red) are similar to rules for variables in most programming languages.
* They can be alphanumeric, but should always begin with a letter.
* They are case sensitive
* They can not contain special characters except underscores.
I worked on it and completed all the conditions except "another json". Hee is my code
import re
import string
class parser(object):
fp=open('jsondata.txt','r')
str=fp.read()
def __init__(self):
print "Name of the file Opened is :",self.fp.name
print "Contents of the file :\n",self.str
def rule1(self,str):
if self.str[:1].startswith('{'):
if self.str[:-1].endswith('}'):
print "RULE 1\n",
print "first character is '{' last character is '} : passed rule1\n"
else:
print "Not a JSON data"
def splitdata(self):
self.str=self.str[1:]
self.str=self.str[:-2]
print self.str
#Storing the words of string in a list
self.list1=[]
self.list1=re.split(',',self.str)
self.list2=[]
self.list3=[]
for i in self.list1:
self.list2=list(re.split(':',i))
self.list3.extend(list(self.list2))
self.left_list=[]
self.right_list=[]
self.i=0
self.left_list=list([self.list3[i] for i in range(len(self.list3)) if i % 2 == 0])
self.right_list=list([self.list3[i] for i in range(len(self.list3)) if i % 2 == 1])
print "DATA SPLIT"
print "Left elements of the json data:",self.left_list
print "Right elements of the json data:",self.right_list,"\n"
def left_parse(self):
"""we gona check "This part of the string":"This part will be checked in next function"\
Conditions imposed on left part of string:\
1.starts and ends with ""\
2.Starts with Alphabet\
3.Contains No special characters except unserscore _"""
for i in range(len(self.left_list)):
self.str1=self.left_list[i]
if self.str1.startswith('"') and self.str1.endswith('"')\
and self.str1[1].isalpha():
self.str2=self.str1[1:]
self.str3=self.str2[:-1]
print "Left side content:",self.str3
print "Status : Valid"if re.match("^[a-zA-Z0-9_]*$", self.str3) else "Invalid"
else:
print "Left side content:",self.str1
print "Status: Invalid"
obj=parser()
obj.rule1(str)
obj.splitdata()
obj.left_parse()
Now the problem is When i tried to check my right_list, whenever i get Another json data then the output goes wild. Can anyone please help me. i framed left_list and right_list based on splitting(comma and colon).
While parsing json inside a json this logic seems not working...