Storage Methods Using JSON in Computer Science Lectures

compsci 105 ss 2015 principles of computer science n.w
1 / 14
Embed
Share

"Explore the use of JavaScript Object Notation (JSON) for storing and transmitting data efficiently in computer science lectures. Learn about the advantages of JSON, its format, and its applications in Python programming and web development."

  • JSON
  • Computer Science
  • Lectures
  • Python
  • Data Format

Uploaded on | 0 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. COMPSCI 105 SS 2015 Principles of Computer Science JSON

  2. Exercise Week 2: Work on Lab2 (Thurs/Fri, classes) Lab2 will be closed at 23:59 Saturday, 17 Jan PreLab02: Exceptions, Json and Complexity Prelab02 will be closed at 23:59 Monday, 19 Jan Exercise Which of the following statements is true? If both except and finally blocks are defined, except block must precede the finally block. A try block is preceded by at least one finally block For each try block there must be at least one except block defined. A try block may be followed by any number of finally blocks a) b) c) d) 2 COMPSCI 105 Lecture 06

  3. Learning outcomes At the end of this lecture, students should be able to: understand what JSON is used for recognise information in JSON format use the Python JSON library to read and write standard Python data types Resources: Tutorials Point: JSON with Python http://www.tutorialspoint.com/json/json_python_example.htm Python Documentation https://docs.python.org/3.3/library/json.html 3 COMPSCI 105 Lecture 06

  4. Question? Given a particular set of data, how do you store it permanently? What do you store on disk? What format? Can you easily transmit over the web? Will it be readable by other languages? Can humans read the data? Examples: A square A dictionary state num: 3 4 den: methods 4 COMPSCI 105 Lecture 06

  5. Storage using plain text Advantages Human readable (good for debugging / manual editing) Portable to different platforms Easy to transmit using web Disadvantages Takes more memory than necessary Use a standardized system -- JSON Makes the information more portable 5 COMPSCI 105 Lecture 06

  6. JavaScript Object Notation Text-based notation for data interchange Human readable Object Unordered set of name-value pairs names must be strings { name1 : value1, name2 : value2, , nameN : valueN } Array Ordered list of values [ value1, value2, valueN ] 6 COMPSCI 105 Lecture 06

  7. Example01.py Writing JSON using Python json.dumps( data ) Accepts Python object as an argument Returns a string containing the information in JSON format Typically write this string to a file def write(data, filename): file = open(filename, 'w') str_out = json.dumps(data) file.write(str_out) file.close() 7 COMPSCI 105 Lecture 06

  8. Example01.py Reading JSON using Python json.loads( data ) Accepts string as an argument The string should be in JSON format Returns a Python object corresponding to the data Double quotes "Hello World" hello.txt def read(filename): file = open(filename) str_in = file.read() file.close() data = json.loads(str_in) return data write('Hello World', 'hello.txt') print(read('hello.txt')) 8 COMPSCI 105 Lecture 06

  9. Example02.py Example 2: Writing a dictionary Create a dictionary my_dict = {'Angela':'86620','adriana':'87113, 'ann':'84947'} file_name = 'test_dict.txt' write(my_dict, file_name) {"ann": "84947", "adriana": "87113", "Angela": "86620"} print(read(file_name)) 9 COMPSCI 105 Lecture 06

  10. Example03.py Writing JSON using pretty printing json.dumps( data ) A dictionary {'b': ['HELLO', 'WORLD'], 'a': ['hello', 'world']} json.dumps( data, indent=4, sort_keys=True ) Formats the output over multiple lines { "a": [ "hello", "world" ], "b": [ "HELLO", "WORLD" ] } Double quotes 10 COMPSCI 105 Lecture 06

  11. What about user-defined classes? Point class class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y def __str__(self): return str(self.x) + ',' + str(self.y) Can create a dictionary to store state information then use JSON value of y value of x p = Point(2, 3) my_dict = {'__class__': 'Point', 'x' : p.x, 'y' : p.y} 11 COMPSCI 105 Lecture 06

  12. What about user-defined classes? Can use json to read and extract the state information file_name = 'test_point.txt' write(my_dict, file_name) { "__class__": "Point", "x": 2, "y": 3 } Example: data = read(file_name) result = Point( data['x'], data['y'] ) print (result) 12 COMPSCI 105 Lecture 06

  13. Exercise Given a Square class, write methods that dump and read JSON import json import io class Square: def __init__(self, len): self.side_length = len def __str__(self): #write your code here 13 COMPSCI 105 Lecture 06

  14. Summary JSON is a standard way to exchange data Easily parsed by machines Human readable form JSON uses dictionaries and lists Dictionaries are unordered Lists are ordered Symbols used in JSON are the same as Python Double quotes used for strings 14 COMPSCI 105 Lecture 06

Related


More Related Content