I have been spending hours to convert the space indented text to JSON. In my raw text data, nested values are indented by 4 spaces and key:value are indented by 2 spaces. I am trying to make a JSON out of them.
My attempt
import json
from pprint import pprint
lines = raw_text.strip().split('n')
result = {}
stack = [(0, result)]
for line in lines:
indent, content = line.split(':', 1)
indent_level = len(indent) - len(indent.lstrip())
key = content.strip()
value = None if key == "null" else {}
print(indent_level, stack[-1][0])
while indent_level <= stack[-1][0]:
stack.pop()
parent = stack[-1][1] # i am getting index error
parent[key] = value
stack.append((indent_level, value))
j = json.dumps(result, indent=4)
Raw data
text = r"""
h0:
h1: de8
h1b: null
h1c:
h2:
h3: A
h3a: S
h3b:
h4:
h5: 81
h1b:
h1bi: null
h1bii: null
"""
Required
j = {
"h0": {
"h1a": "de8",
"h1b": "null",
"h1c": {
"h2a": {
"h3a": "A",
"h3b": "S",
"h3c": {
"h4a": {
"h5a": "81"
}
}
}
},
"h1d": {
"h1di": "null",
"h1dii": {
"h2di": "hello",
"h2dii": "hi"
}
}
}
}
3
Answers
As per the suggestion of @DasaniT, this worked for me
You can use the
yaml
package: https://pypi.org/project/PyYAML/The code will look like something like this:
and the output is:
Here you go (
without external lib
)output