0% found this document useful (0 votes)
24 views35 pages

19 Json

Uploaded by

Todd Hofer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views35 pages

19 Json

Uploaded by

Todd Hofer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JSON

Programming
Copyright © 2024 by
Robert M. Dondero, Ph.D.
Princeton University

1
Objectives
• We will cover:
– JSON
– JSON programming
– XML/JSON and AJAX

2
Agenda
• JSON
• JSON programming
• XML/JSON and AJAX

3
JSON
• JSON (JavaScript Object Notation)
– Like XML:
• Textual; human readable
• Hierarchical
– Unlike XML:
• Derived from JavaScript array and object
notation

4
JSON
• JSON document
– A source code expression of a JavaScript
data structure
• JavaScript data structure can consist of:
– Strings, Numbers, Booleans, or null
– Objects or arrays having properties that are Strings,
Numbers, Booleans, null, objects, or arrays
• See [Link]

5
JSON
JSON for data comm

JSON
Schema

Process1 Process2

App1 JSON doc App2

JSON JSON
Generator Parser

6
JSON
• Examples in this lecture:
– In Python
• Appropriate for JSON programming on the
server-side of a Web app
– In JavaScript
• Appropriate for JSON programming on the
client-side of a Web app (i.e., in a browser)

7
Agenda
• JSON
• JSON programming
• XML/JSON and AJAX

8
JSON Programming
• writebooksjson programs

– Write all books in [Link]

9
JSON Programming
• See [Link]

$ python [Link]
Author: Kernighan
Title: The Practice of Programming JSON Python
Price: 40.74 dollars
array list
Author: Kernighan object dict
Title: The C Programming Language
Price: 24.99 dollars
Number int, float
String str
Author: Sedgewick
Title: Algorithms in C
Boolean bool
Price: 61.59 dollars null None
$

10
JSON Programming
• See [Link]

$ node [Link]
Author: Kernighan
Title: The Practice of Programming
Price: 40.74 dollars

Author: Kernighan
Title: The C Programming Language
Price: 24.99 dollars

Author: Sedgewick
Title: Algorithms in C
Price: 61.59 dollars

11
JSON Programming
• roundtrip programs

– Translate from JSON to data structure


– Translate from data structure to JSON

12
JSON Programming
• See [Link]

$ python [Link] [Link]


[{"author": "Kernighan", "title": "The
Practice of Programming", "price": 40.74,
"currency": "dollars"}, {"author":
"Kernighan", "title": "The C Programming
Language", "price": 24.99, "currency":
"dollars"}, {"author": "Sedgewick",
"title": "Algorithms in C", "price":
61.59, "currency": "dollars"}]
$

13
JSON Programming
• See [Link]

$ node [Link]
[{"author":"Kernighan","title":"The Practice of
Programming","price":40.74,"currency":"dollars"},
{"author":"Kernighan","title":"The C Programming
Language","price":24.99,"currency":"dollars"},{"a
uthor":"Sedgewick","title":"Algorithms in
C","price":61.59,"currency":"dollars"}]

14
JSON Programming
• XML pros
– Appropriate for data comm and publishing
• JSON pros
– Compact
– Easy to handle in JavaScript
– Easy to handle in Python, …
– OK to handle in Java, …

15
Aside: Data Comm Formats
• Data comm formats
– Binary:
• Pickled Python objects, …
– Human readable:
• plain text, HTML, XML, JSON, …
• See:
– [Link]
data_serialization_formats

16
Agenda
• JSON
• JSON programming
• XML/JSON and AJAX

17
XML/JSON and AJAX
• Recall PennyJQuery3 app

18
XML/JSON and AJAX
• Recall PennyJQuery3 app

19
XML/JSON and AJAX
• Recall PennyJQuery3 app
– [Link]
– [Link], [Link]
– [Link], [Link]
– [Link]
– [Link]
– [Link]

20
XML/JSON and AJAX
• Observation
– Server sends AJAX response as HTML
– Server could send AJAX response as XML or
JSON

21
XML/JSON and AJAX
• See PennyJQueryXml app
– [Link]
– [Link], [Link]
– [Link], [Link]
– [Link]
– [Link]
– [Link]

22
XML/JSON and AJAX
• See PennyJQueryJson app
– [Link]
– [Link], [Link]
– [Link], [Link]
– [Link]
– [Link]
– [Link]

23
Question (lecture19)
• So the server could send XML/JSON
fragments instead of HTML fragments.
Why should the server send XML/JSON
fragments instead of HTML fragments?
(No fair looking at the following slides.)

– Browse to
[Link] to
answer

24
XML/JSON and AJAX
• Answer 1: Sending XML/JSON is more
secure
AJAX request

Browser Application
HTML fragment

Browser inserts HTML fragment into DOM


Dangerous!

25
XML/JSON and AJAX
• Answer 2: Sending XML or JSON defines
a convenient API
– Example…

26
XML/JSON and AJAX
• See [Link]
$ python [Link] localhost 55555 ker
Kernighan
The Practice of Programming
40.74

Kernighan
The C Programming Language
24.99

27
Summary
• We have covered:
– JSON
– JSON programming
– XML/JSON and AJAX
• See also:
– Appendix: JSON Checking

28
Appendix:
JSON Checking

29
JSON Checking
• To run the example Python programs in
this appendix:
– python –m pip install jsonschema

30
JSON Checking: Well-Formedness
• Computer science jargon…
– A JSON document is well-formed iff it
conforms to the JSON specification

31
JSON Checking: Well-Formedness
• See [Link] (revisited)
• See [Link]
• See [Link]

$ python [Link] [Link]


The document is well-formed.
$ python [Link] [Link]
Expecting ',' delimiter: line 15 column 7 (char 353)
$

32
JSON Checking: Validity
• JSON Schemas
– Internet draft
• Published by Internet Engineering Task Force
• 12th draft released 2022 June 10
• See [Link]
• See Wikipedia JSON page
– A JSON schema defines whether a JSON
doc is valid

33
JSON Checking: Validity
• See [Link]
• See [Link] (revisited)
• See [Link]
• See [Link]

34
JSON Checking: Validity
$ python [Link] [Link] [Link]
The document is well-formed.
The document is valid.
$ python [Link] [Link] [Link]
The document is well-formed.
'currency' is a required property

Failed validating 'required' in schema['items']:


{'properties': {'author': {'type': 'string'},
'currency': {'type': 'string'},
'price': {'type': 'number'},
'title': {'type': 'string'}},
'required': ['author', 'title', 'price', 'currency'],
'type': 'object'}

On instance[2]:
{'author': 'Sedgewick', 'price': 61.59, 'title': 'Algorithms in
C'}
$

35

You might also like