Parsing and Generating JSON Files Flashcards
What programs can you use to parse and generate JSON files?
C#, Python, and many other programs
C# has 2 namespaces you can use to work with JSON files.
You can both create and parse JSON files using commands in these namespaces
In C #, ____ and _____ have specific meanings for JSON
Serialization
Deserialization
Serialization
Writing objects to JSON format
Deserialize
Read JSON into C# objects
How to write C# code to work with JSON
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
JavaScript Serializer and Deserializer
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.
JSON Example (and video on next side)
https://lms.devry.edu/lms/video/player.html?video=1_r70dsdk3
JSON and Python
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):
JSON and Python Objects
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)
Encoding with a Separate Function
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)
Encoding Shortcut
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)
Loading a JSON File
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
Python Example Video
https://lms.devry.edu/lms/video/player.html?video=1_bnixdjxi