JSON - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSON Python

JSON Python

shape Description

Python is an object-oriented programming language, Python is a dynamic, interpreted dialect and there are no type declarations of methods and functions. It makes the code short and adaptable. Following are some important features of Python. All the Python files will be saved with the extension .py. Following is the basic example for the Python language. Save the following code with test.py . [html]print "Hello, Python!"[/html] Now compile the code as follows. [html]$ python test.py[/html] Output: Now the result will be as folows. [html]Hello, Python![/html]

JSON encode And decode With Python

shape Description

Python makes it easy to work with JSON records. This module ought to be incorporated (inherent) inside Python establishment, and don't have to introduce any outside modules. The main thing require with a specific end goal to utilize this module is to import it. JSON to Python: Perusing JSON implies changing over JSON into a Python esteem. As specified over, the json library parses JSON into a word reference or rundown in Python. Keeping in mind the end goal to do that, client require the loads() function (load from a string). [html] import json jsonData = '{"name": "Frank", "age": 39}' jsonToPython = json.loads(jsonData) [/html] Python to JSON : The method dumps() will be used to convert from python to JSON. [html] import json pythonDictionary = {'name':'Bob', 'age':44, 'isEmployed':True} dictionaryToJson = json.dumps(pythonDictionary) [/html]

Encoding JSON in Python

The functionality of the function encode() is to convert the Python object into the string format of the JSON. Following is the syntax for the encode() function. [html]demjson.encode(self, obj, nest_level=0)[/html] Following is an example. [html] #!/usr/bin/python import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json [/html] In the above example, demson is nothing but the module of Python for decoding , encoding and for the purpose of syntax checking. Output: Now compile the code result will be as follows. [html][{"a":1,"b":2,"c":3,"d":4,"e":5}][/html]

Decoding JSON in Python

The functionality of the function decode() is to convert the string format of the JSON into python object. Following is the syntax for the decode() function. [html]demjson.decode(self, txt)[/html] Following is an example. [html] #!/usr/bin/python import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print text [/html] In the above example, demson is nothing but the module of Python for decoding , encoding and for the purpose of syntax checking. Output: Now compile the code result will be as follows. [html]{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}[/html]

Summary

shape Key Points

  • Python language underpins graphical user interface applications.
  • The function encode() is used to convert python object into string form of the JSON.
  • The function decode() is used to convert JSON string into object form of the python.