Home Search for specific string in JSON file using python
Reply: 0

Search for specific string in JSON file using python

Milister
1#
Milister Published in 2018-02-13 16:00:28Z

I have json file

{
    "glossary": {
        "name": "john",
                "GlossDiv": {
            "machinename": "my-machine",
                        "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                                        "SortAs": "SGML",
                                        "GlossTerm": "Standard Generalized Markup Language",
                                        "Acronym": "SGML",
                                        "Abbrev": "ISO 8879:1986",
                                        "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                                                "GlossSeeAlso": ["GML", "XML"]
                    },
                                        "GlossSee": "markup"
                }
            }
        }
    }
}

i want to return only string after "name": (john) and string after word "machinename" (my-machine) without quotes

so far i created this part:

#!/bin/python


lines = [] #Declare an empty list named "lines"


with open ('./my.json', 'rt') as in_file:  # Open file
 contents = in_file.read()# Read the entire file into a variable named contents.
#print(contents) # Print contents.
print contents.split("name",1)[1]

And this is result (it gives me all the lines after "name"

": "john",
                "GlossDiv": {
            "machinename": "my-machine",
                        "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                                        "SortAs": "SGML",
                                        "GlossTerm": "Standard Generalized Markup Language",
                                        "Acronym": "SGML",
                                        "Abbrev": "ISO 8879:1986",
                                        "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                                                "GlossSeeAlso": ["GML", "XML"]
                    },
                                        "GlossSee": "markup"
                }
            }
        }
    }
}

So i just want from this json file to get stings john and my-machine

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.601889 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO