Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

I'm a little bit confused with JSON in Python. To me, it seems like a dictionary, and for that reason, I'm trying to do that:

  "glossary":

   { 

     "title":

       "example glossary", "GlossDiv":

        {

         "title": "S", "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" 

             } 

          } 

       } 

    }

 }

But when I do print dict(json), it gives an error.

How can I transform this string into a structure and then call json["title"] to obtain "example glossary"?

1 Answer

0 votes
by (106k points)

If you want to convert string to JSON using Python you can use the json.loads() and json.dumps()function which will do your task below is the code which shows how to use the json.load() function:-

import json

a= {'id': 1, 'name': 'vishal'}

b= json.dumps(a)

c= json.loads(b)

print(c['id'], c['name'])

image

Related questions

0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (12.7k points)
0 votes
2 answers
asked Oct 4, 2019 in Python by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 5, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...