Amir Saar Amir Saar - 4 months ago 20
JSON Question

Create a powershell dictionary

I would like to copy a file's data that contains a dictionary structure into PowerShell. I've pasted the content and now I have a variable called

$dns
, this is its content:

{
"ips": {
"10.20.30.40": [
{
"rhost": "DNS Name1.",
"rdata": [
"10.20.30.40"
],
"rrtype": "A (1)",
"ttl": 86400,
"geo": null,
"source": "DNSProvider1"
}
],
"40.50.60.70": [
{
"rhost": "DNS Name2.",
"rdata": [
"40.50.60.70"
],
"rrtype": "A (1)",
"ttl": 86400,
"geo": null,
"source": "DNSProvider1"
}
]
}
}


I want to run some manipulations on it, should I convert it to an Object or something else?

Just to clarify myself, when I use this kind of object in Python, I can use this object like this:

dns['ips']


The result looks like this:

{
'10.20.30.40':
[
{
'geo': null,
'rdata': ['10.20.30.40'],
'rhost': 'DNS Name1.',
'rrtype': 'A (1)',
'source': 'DNSProvider1',
'ttl': 86400
}
],
'40.50.60.70':
[
{
'geo': null,
'rdata': ['40.50.60.70'],
'rhost': 'DNS Name2.',
'rrtype': 'A (1)',
'source': 'DNSProvider1',
'ttl': 86400
}
]
}




Answer

You are looking for the ConvertFrom-Json cmdlet:

($dns | ConvertFrom-Json).ips