Is there a way to extract the structural details of a json file using Python?
I have a file that looks something like this:
{
"help": "https://?name=package_search",
"success": true,
"result": {
"count": 47,
"facets": {},
"results": [
{
"author": "ggm",
"author_email": "ggm@____.nl",
"creator_user_id": "x_x_x_x" }]}}
Whereas I only want to see a structure that would be something like this:
{
{
[
{
}
{
}
]
}
}
Is this possible to achieve with Python?
CodePudding user response:
You can use a regex (assuming the data is still in string form and not yet parsed to a dict):
import re
s = """{
"help": "https://?name=package_search",
"success": true,
"result": {
"count": 47,
"facets": {},
"results": [
{
"author": "ggm",
"author_email": "ggm@____.nl",
"creator_user_id": "x_x_x_x" }]}}"""
print(re.sub(r"[^{}[\]\n]", ' ', s))
Will give:
{
{
{}
[
{
}]}}
This uses re.sub
which is the regex version of replace
and basically says: "Replace everything except brackets and new line with a space" (see the demo).