JSON Serialization and Deserialization
Learn about JSON serialization and deserialization in Python and Powershell.
We'll cover the following...
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.
Press + to interact
$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.
Press + to interact
import jsonjson_string = '{"first": "Prateek", "last":"Singh"}'## loads and parses the JSONparsed_json = json.loads(json_string)## accessing the parsed JSON like a hash tableprint(parsed_json['first'])