Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate a file that only contains the structural parts (i.e. brackets) of a json file without other contents

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
   {
      [
       {
        }
       {
        }
         ]
              }
                   }

Is this possible to achieve with Python?

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading