Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves issue #45 #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions extruct/jsonStringFixer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
def addMissingQuotesInJsonString(s):

s = s.split("\n")
finalstring=''
for i in range(len(s)):
s1 = s[i]
s2 = ''
startflag=0
j=0
while j<len(s1):
if startflag==0:
while True:
if j==len(s1):
break
if (ord(s1[j])>=48 and ord(s1[j])<=57) or (ord(s1[j])>=65 and ord(s1[j])<=90) or (ord(s1[j])>=97 and ord(s1[j])<=122):
s2=s2+'"'+s1[j]
j=j+1
startflag=1
break
elif s1[j]=='"':
s2=s2+s1[j]
j=j+1
startflag=1
break
else:
s2=s2+s1[j]
j=j+1


elif startflag==1:
while True:
if j==len(s1):
s2=s2+'"'
startflag=0
break
if s1[j]=="]" or s1[j]==";" or s1[j]=="," or (s1[j]==":" and s1[j+1]!="/") or s1[j]=="}" :
s2=s2+'"'+s1[j]
j=j+1
startflag=0
break
elif s1[j]=='"':
s2=s2+s1[j]
j=j+1
startflag=0
break
else:
s2=s2+s1[j]
j=j+1
if startflag==1 and j==len(s1):
s2=s2+'"'
startflag=0

finalstring = finalstring + s2 +'\n'

s = finalstring.split("\n")
finalstring=''
for i in range(len(s)):
s1 = s[i]
s2 = ''
colonflag=0
j=0
while j<len(s1):
if colonflag==0:
while True:
if j==len(s1):
break
elif s1[j]==":":
s2=s2+s1[j]
j=j+1
colonflag=1
break
else:
s2=s2+s1[j]
j=j+1


elif colonflag==1:
while True:
if j==len(s1):
s2 =s2+'null'
colonflag=0
break
if s1[j]=="]" or s1[j]==";" or s1[j]=="," :
s2=s2+'null'+s1[j]
j=j+1
colonflag=0
break
elif s1[j]!=" ":
s2=s2+s1[j]
j=j+1
colonflag=0
break
else:
s2=s2+s1[j]
j=j+1
if colonflag==1 and j==len(s1):
s2 =s2+'null'
colonflag=0

finalstring = finalstring + s2 +'\n'
return finalstring
2 changes: 2 additions & 0 deletions extruct/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re

import lxml.etree
from jsonStringFixer import addMissingQuotesInJsonString

from extruct.utils import parse_html

Expand All @@ -33,6 +34,7 @@ def _extract_items(self, node):
# TODO: `strict=False` can be configurable if needed
data = json.loads(script, strict=False)
except ValueError:
script = addMissingQuotesInJsonString(script)
# sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
data = json.loads(
HTML_OR_JS_COMMENTLINE.sub('', script), strict=False)
Expand Down