JSON and Python Data Exchange

json python n.w
1 / 19
Embed
Share

Learn about JSON, a standard format for data exchange, and its relation to Python. Explore the syntax of JSON, its key features, and how it is used in various programming languages. Get insights into creating and working with JSON objects and values in Python.

  • JSON
  • Python
  • Data exchange
  • Programming languages
  • Syntax

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. JSON & Python By Dr. Ziad Al-Sharif

  2. What is JSON? What is JSON? JSON is a standard format for data exchange: stands for JavaScript Object Notation Originally inspired by JavaScript Nowadays, most languages provide JSON pressers Generally, JSON is in string or text format Textual Human Readable you can open/edit any JSON file using any text editor such as notepad, notepad++, etc.) The syntax of JSON: JSON is written as key and value pair A collection of name/value pairs.

  3. Languages Languages ActionScript C C++ C# Cold Fusion Delphi E Erlang Java JavaScript Lisp Objective-C Objective CAML Perl PHP Python Rebol Ruby Scheme Squeak

  4. JSON Object JSON Object An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace. Each name is followed by colon (:) the name/value pairs are separated by comma (,)

  5. Object Object Objects are unordered containers of key/value pairs equivalent to dict() in Python Objects are wrapped in { } Commas , separate key/value pairs Colons : separate keys and values Keys are strings Values are any valid JSON value number, dict, list, string, etc.

  6. Values Values Value can be any of Strings Numbers int float Booleans true false Objects Arrays null A value that isn't anything

  7. Strings Strings Sequence of 0 or more Unicode characters No separate character type Double quotes, E.g. ?? not the single quote Backslash escapement

  8. Numbers Numbers Integer Real Scientific No octal or hex No NaN or Infinity Use null instead

  9. Object { "name": "Ahmed", "class": "Python", "birthday": "2000-02-10", "graduated": false, "age": 20, "grades": { "first": "10", "second": 25, "final": 37 }, "car": null, "favorite_foods": [ "cookie", "fish", "chips" ] }

  10. Array Arrays are ordered sequences of values equivalent to the list() in python Arrays are wrapped in [] Commas , separate values JSON does not talk about indexing. An implementation can start array indexing at 0 or 1.

  11. Array Array ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ] [ [0, -1, 0], [1, 0, 0], [0, 0, 1] ]

  12. Rules: Encoder vs. Decoder A JSON encoder must only produce well-formed JSON text. A JSON decoder must accept all well-formed JSON text. A JSON decoder may also accept non-JSON text. Be conservative in what you do, be liberal in what you accept from others. JSON's simple values are the same as used in programming languages. No restructuring is required: JSON's structures look like conventional programming language structures. JSON's object is object, dictionary, lists ... JSON's array is array, vector, sequence, list...

  13. JSON Library in Python JSON Library in Python To perform JSON related operations like encoding and decoding in Python you need first to import JSON library into your .py file. import json Then, you may use the following methods from your JSON module Method Description dumps() Encodes a python structure into JSON and returns a string dump() Encodes a python structure into JSON and writes the results into the file loads() Decode the JSON string and generates an equivalent python structure load() Decode the contents of the given JSON file while reading its contents and generates an equivalent python structure

  14. Ex. Reading a JSON File sample.json [ { "name": "Ahmed", "grades": { "first": 18, "second": 24, "final": 42 } } , { "name": "Ali", "grades": { "first": 10, "second": 25, "final": 37 } } , { "name": "Hamza", "grades": { "first": 15, "second": 22, "final": 47 } } ] import json f = open('sample.json','r') data = json.load(f) f.close() import pprint as pp pp.pprint(data)

  15. Ex. Writing Python Data structure into a JSON File import json d = {1:"one", 2:"two", 3:"three"} data = json.dumps(d) f = open('dumped-from-python.json','w') json.dump(d,f) f.close() dumped-from-python.json { 1: "one", 2: "two", 3: "three"}

  16. Python to JSON (Encoding) Converting Python data to JSON is called an Encoding operation. Encoding is done with the help of JSON library method dumps() dumps() method converts dictionary object of python into JSON string data format. JSON encoder for Python data structures. Supports the following objects and types by default: Python JSON dict object list, tuple array str string int, float number True true False false None null

  17. JSON to Python (Decoding) JSON string decoding is done with the help of inbuilt method loads() & load() of JSON library in Python. Here translation table show example of JSON objects to Python objects which are helpful to perform decoding in Python of JSON string. Performs the following translations in decoding by default: JSON Python object dict array list string str number (int) int number (real) float true True false False null None

  18. Summary Summary JSON Language Independent. Text-based. Light-weight. Easy to parse. Not a document format.

  19. References References JSON JSON encoder and decoder https://docs.python.org/3/library/json.html Introducing JSON https://www.json.org/json-en.html

More Related Content