I have the following which I’d like to parse it into JSON. The class has a list of item object also
JavaScript
x
17
17
1
class Item(JSONEncoder):
2
def __init__(self):
3
self.Type = ''
4
self.Content = ''
5
self.N = None
6
self.Parent = None
7
self.Items = []
8
9
def reprJSON(self):
10
d = dict()
11
for a, v in self.__dict__.items():
12
if (hasattr(v, "reprJSON")):
13
d[a] = v.reprJSON()
14
else:
15
d[a] = v
16
return d
17
So, when I try to parse the instance of Item class, root.reprJSON()
I get the following result.
JavaScript
1
75
75
1
{'Type': 'root',
2
'Content': '',
3
'N': 'root',
4
'Parent': None,
5
'Items': [<Item.Item at 0x10575fb3c88>,
6
<Item.Item at 0x10575fb3e10>,
7
<Item.Item at 0x10575fb3eb8>,
8
<Item.Item at 0x10575fbc080>,
9
<Item.Item at 0x10575fbc2b0>,
10
<Item.Item at 0x10575fc6a20>,
11
<Item.Item at 0x10575fc6a58>,
12
<Item.Item at 0x10575fc6b70>,
13
<Item.Item at 0x10575fc6be0>,
14
<Item.Item at 0x10575fc6c50>,
15
<Item.Item at 0x10575fc6da0>,
16
<Item.Item at 0x10575fc6fd0>,
17
<Item.Item at 0x10575fcb128>,
18
<Item.Item at 0x10575fcb358>,
19
<Item.Item at 0x10575fcba90>,
20
<Item.Item at 0x10575fcbb00>,
21
<Item.Item at 0x10575fcbb70>,
22
<Item.Item at 0x10575fcbc18>,
23
<Item.Item at 0x10575fcbda0>,
24
<Item.Item at 0x10575fcbfd0>,
25
<Item.Item at 0x10575fd3208>,
26
<Item.Item at 0x10575fd34a8>,
27
<Item.Item at 0x10575fd3550>,
28
<Item.Item at 0x10575fd35c0>,
29
<Item.Item at 0x10575fd36d8>,
30
<Item.Item at 0x10575fd37f0>,
31
<Item.Item at 0x10575fd3898>,
32
<Item.Item at 0x10575fd3940>,
33
<Item.Item at 0x10575fd39b0>,
34
<Item.Item at 0x10575fd3a20>,
35
<Item.Item at 0x10575fd3ac8>,
36
<Item.Item at 0x10575fd3b70>,
37
<Item.Item at 0x10575fd3c88>,
38
<Item.Item at 0x10575fd3d68>,
39
<Item.Item at 0x10575fd3dd8>,
40
<Item.Item at 0x10575fd3e10>,
41
<Item.Item at 0x10575fd3ef0>,
42
<Item.Item at 0x10575fdc080>,
43
<Item.Item at 0x10575fdc0b8>,
44
<Item.Item at 0x10575fdc128>,
45
<Item.Item at 0x10575fdc1d0>,
46
<Item.Item at 0x10575fdc240>,
47
<Item.Item at 0x10575fdc390>,
48
<Item.Item at 0x10575fdc438>,
49
<Item.Item at 0x10575fdc550>,
50
<Item.Item at 0x10575fdc5c0>,
51
<Item.Item at 0x10575fdc630>,
52
<Item.Item at 0x10575fdc6a0>,
53
<Item.Item at 0x10575fdc6d8>,
54
<Item.Item at 0x10575fdc780>,
55
<Item.Item at 0x10575fdc908>,
56
<Item.Item at 0x10575fdc9e8>,
57
<Item.Item at 0x10575fdca58>,
58
<Item.Item at 0x10575fdcac8>,
59
<Item.Item at 0x10575fdcb00>,
60
<Item.Item at 0x10575fdcba8>,
61
<Item.Item at 0x10575fdccc0>,
62
<Item.Item at 0x10575fdcd30>,
63
<Item.Item at 0x10575fdcda0>,
64
<Item.Item at 0x10575fdce48>,
65
<Item.Item at 0x10575fdceb8>,
66
<Item.Item at 0x10575fdcf28>,
67
<Item.Item at 0x10575fe22e8>,
68
<Item.Item at 0x10575fe2828>,
69
<Item.Item at 0x10575fe2940>,
70
<Item.Item at 0x10575fe2b70>,
71
<Item.Item at 0x10575fe2be0>,
72
<Item.Item at 0x10575fe2c88>,
73
<Item.Item at 0x10575fe2cc0>,
74
<Item.Item at 0x10575fe2cf8>]}
75
But I’d like to get the values of those item also into a single json object. I don’t know how to do it, would appreciate any help. Thank you
Edit
Following code create an instance of item class and filled it with data.
JavaScript
1
103
103
1
def Crawl(parsedPDF):
2
3
soup = BeautifulSoup(parsedPDF, "html.parser")
4
5
6
root = Item()
7
root.Type = "root"
8
root.N = "root"
9
parent = root
10
head = root
11
body = RemoveEmptyTags(soup.body)
12
13
14
for tag in body:
15
elements = RemoveEmptyChild(tag.contents)
16
for element in elements:
17
if element.name == "head":
18
head = CreateHeading(root, parent, element)
19
parent = head.Parent
20
elif element.name == "p":
21
AddParagraph(head, element)
22
elif element.name == "figure":
23
pass
24
elif element.name == "figdesc":
25
pass
26
elif element.name == "table":
27
#elem = AddElement(head, element)
28
pass
29
else:
30
#elem = AddElement(head, element)
31
pass
32
33
pass
34
35
36
return root
37
38
39
def AddParagraph(head, element):
40
# split the paragraph into multiple lines based on alphabetize bullet points
41
lines = split_with_AplhabetizeBullets(element.text, '.s((.*?)s)')
42
for line in lines:
43
item = Item()
44
item.Content = line
45
item.Type = element.name
46
item.Parent = head
47
head.Items.append(item)
48
49
50
51
52
def CreateHeading(root, parent, element):
53
item = Item()
54
item.Content = element.text
55
item.Type = element.name
56
item.Parent = parent
57
58
try:
59
item.N = element["n"]
60
except:
61
pass
62
63
if item.N is None:
64
bracketTextLength = 0
65
try:
66
result = re.search(r'(.*?)',item.Content)
67
bracketTextLength = len(result.group)
68
except:
69
pass
70
71
item.N = item.Content
72
# to check if the heading without 'N' is a heading or its a subheading
73
if len(item.Content) > 3 and bracketTextLength == 0:
74
root.Items.append(item)
75
item.Parent = item
76
pass
77
else:
78
parent.Items.append(item)
79
pass
80
81
82
83
84
85
else: # item.N is not None
86
if parent.N is None:
87
item.Parent = item
88
parent = item.Parent
89
pass
90
91
#else: # if the new heading sharing the same reference as of its parent then
92
if parent.N in item.N[:len(parent.N)]:
93
parent.Items.append(item)
94
pass
95
96
else: # if the new heading has no parent then add it into root
97
root.Items.append(item)
98
item.Parent = item
99
pass
100
101
102
return item
103
Advertisement
Answer
Looking at your code you can use this demo solution in your code as I’m storing objects of Demo class in the Items list. You need to write serialize()
and dumper()
methods in Items class, and also changes need to be done in reprJSON
method for iteration on Items list.
JavaScript
1
55
55
1
from json import JSONEncoder
2
3
class Demo():
4
def __init__(self):
5
self.name = ''
6
self.demolist = []
7
8
class Item(JSONEncoder):
9
10
def __init__(self):
11
# super().__init__()
12
self.Type = ''
13
self.Content = ''
14
self.N = None
15
self.Parent = None
16
self.Items = []
17
18
def reprJSON(self):
19
d = {}
20
for a, v in self.__dict__.items():
21
if isinstance(v, list):
22
for i in v:
23
if d.get(a, []) == []:
24
d[a] = []
25
d[a].append(self.dumper(i))
26
else:
27
d[a].append(self.dumper(i))
28
else:
29
d[a] = v
30
return d
31
32
def serialize(self):
33
return self.__dict__
34
35
@staticmethod
36
def dumper(obj):
37
if "serialize" in dir(obj):
38
return obj.serialize()
39
return obj.__dict__
40
41
42
43
44
itemobj = Item()
45
d1 = Demo()
46
d2 = Demo()
47
d1.name = 'akash'
48
d1.demolist = [{'good':[4,6,5],'yyy':'why'},{'ho':{'ksks':'333'}}]
49
d2.name = 'heheh'
50
d2.demolist = [4,6,1111]
51
itemobj.Items.extend([d1,d2])
52
53
from pprint import pprint
54
pprint(itemobj.reprJSON())
55
Output:
JavaScript
1
9
1
{'Content': '',
2
'Items': [{'demolist': [{'good': [4, 6, 5], 'yyy': 'why'},
3
{'ho': {'ksks': '333'}}],
4
'name': 'akash'},
5
{'demolist': [4, 6, 1111], 'name': 'heheh'}],
6
'N': None,
7
'Parent': None,
8
'Type': ''}```
9