Search⌘ K
AI Features

JSON Serialization and Deserialization

Explore the process of JSON serialization and deserialization in PowerShell and Python. Understand how to convert objects to JSON format and back, using PowerShell cmdlets and Python's json module, enabling effective data storage and manipulation.

Parsing JSON

Parsing data in a JSON format in PowerShell is as easy as piping the JSON string to the ConvertTo-JSON cmdlet. This emdlet will automatically parse the JSON content and return the PowerShell objects, as in the following example.

Python 3.5
$json_string = '{"first": "Prateek", "last":"Singh"}'
## parses the JSON string to objects
$parsed_json = $json_string | ConvertFrom-Json
## accessing the parsed JSON data
$parsed_json.first

Python, on the other hand, has an inbuilt module known as json to parse data in JSON formats and files. Simply import this module and use the loads() function which will return Python objects after parsing the JSON data.

Python 3.5
import json
json_string = '{"first": "Prateek", "last":"Singh"}'
## loads and parses the JSON
parsed_json = json.loads(json_string)
## accessing the parsed JSON like a hash table
print(parsed_json['first'])
...