Parsing and Generating JSON Files Flashcards

1
Q

What programs can you use to parse and generate JSON files?

A

C#, Python, and many other programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

C# has 2 namespaces you can use to work with JSON files.

A

You can both create and parse JSON files using commands in these namespaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In C #, ____ and _____ have specific meanings for JSON

A

Serialization
Deserialization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Serialization

A

Writing objects to JSON format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Deserialize

A

Read JSON into C# objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to write C# code to work with JSON

A

Create a new C# program in Visual Studio using .NET Framework not .NET Core because we are using NuGet packages.

Right-click on References and choose Add Reference, then pick System.WebExtensions

Right-click on References and choose Manage NuGet packages. Search for JSON. Install and Newtonsoft.JSON

You are ready to write C# code to work with JSON

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

JavaScript Serializer and Deserializer

A

The .Serialize() mehtod is used to write JSON to a string or a file. You can write .NET objects to JSON using the following example assuming p1 is an object

string outputJSON = JsonConvert.SerializeObject (pl);

When creating an object, all public properties are serialized. Arrays may be used as well. The .Deserialize() method is used to read JSON from a string and creates an instance of a class

person p2 = JsonConvert.DeserializeObject<Person>(JSONstring);</Person>

Property name matching is case-sensitive. Deserialization to read-only properties is not supported. Comments in the JSON files throw exceptions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JSON Example (and video on next side)

A

https://lms.devry.edu/lms/video/player.html?video=1_r70dsdk3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

JSON and Python

A

Python can parse and generate JSON files. The json.dump() method serializes dict, list, tuple, True, sting, int, float, False, and none.

There is an option to use a json.JSONDecoder if the type is not one of the ones in the list below.

JSON performs the following translations (see screenshot):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

JSON and Python Objects

A

To convert an object to JSON, these steps are needed.
1. Create a Python class
2. Instantiate an object of this class
3. Convert class properties to JSON string or write JSON string to file

The json.dumps() function converts a Python class object to a JSON string.

The code to the right will create output.json.

(“name”: “Anakin”, “age”: 45)

To save the person p1 to a string use the following code

strJson = json.dumps(pl.dict, indent=2)

Python Code
(see screenshot)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Encoding with a Separate Function

A

Python need additional steps to convert objects directly to JSON. Creating a separate encoding method to serializing objects that are not serializable helps to ensure the right type is used for the output. In the example below, the person encoder is the method to serialize the Person object.

Python Code (see screenshot)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Encoding Shortcut

A

Another simpler method is to use .dict to get the dictionary version of the object parameters

In this example, the bolded line of code will convert the object to a Python dictionary
(see screenshot for Python code)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Loading a JSON File

A

Simlary, you can read in a JSON file to a Python object using the json.loads() or json.load() functions.

json.load() is used to read a JSON document from a file and convert it to a dictionary and json.loads() is used to convert a JSON string into a dictionary.

The personDecoder() function takes a dictionary and converts it to a Python object.

see screenshot for python code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Python Example Video

A

https://lms.devry.edu/lms/video/player.html?video=1_bnixdjxi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A