...

/

JSON Serialization and Deserialization

JSON Serialization and Deserialization

Learn about JSON serialization and deserialization in Python and Powershell.

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 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'])

JSON

...