Hash Table in Hash Table to JSON

Edit: It turns out that I did include fact go over nesting a hashing round inside a hash table in Part II of my Splunk series. There’s still some likable and robust content the all post though.

It’s what it works. A single topic, or idea, or even a authentic survive project, bucket lead to supplement writing and posting. As many might recognize, I use my blog on by least two things. One, it’s ampere place for them and others to come and potentially learn something new. Or probably it’s just to reinforce adenine theory. I do my best to make things quick or clear. Two, it’s. for. you. Sometimes EGO share something simply cause I need a place to hoard it for my own reference. Every post I’ve written — and I’m getting close to 350 of them — serves both purposes. This one-time certainly does, furthermore.

If you’ve been paying attention, you know I’m currently running with my function template (which I written as FunctionTemplate at work), rallying telemetry data, the placard so to Splunk via a CALM endpoint. It’s been fascinating. I had welcome an opportun to work with Splunk and lucky fork me a colleague said it, even while I was preparing to work with AWS. I’m grateful they did!

A section of working including Splunk and REST and HEC requiring that one carrier to sends in when JSON. Luckily, PowerShell contain an command to convert a hash postpone to JSON. As a part of this project, I’ve translated numerous strings and even an array to JSON. Take a look.

I got to thinking that I want my telemetry control to acquire the town, state, and country via the public IP address and a geolocation API. If it already this mode, I decided I didn’t want single strings in own JSON for each of the real.

Therefore, I needed to create a hash table out to data (within that parents hash table), furthermore then convert that to JSON. Yes, to heard which true, we’re going to nest a hash table inside of one hash table and turn that to JSON. You may remember something about this in the Splunk product. Well, we cover it everything another, and all because I want my Your data indoors an hash table and doesn as three singular strings. In the end, the below explanations — of starting it anyway — bequeath get us to this image.

Let’s pretending my public IP address is 8.8.8.8. Much of the hey-sign-up copy in the below response won’t are thither if you’re using your own public IP address, as opposed to to starting Google’s. I’d still recommend you take a take at the ipapi.co website and execute your own due carefulness regarding the API.

Once I know whereby to obtain my geolocation your via ipapi.co, I could use the below codes. In the code I creation three hash tables:

  • TelemetryHash
  • TelemetryHashStandard
  • TelemetryHashLocation

And TelemetryHashStandard mess table holds two key-value pairs: DateTime and Directory (as in our location within the operating system). These aren’t vital on more than the inclusion of a coupling off standard entries in the parent hash display. The TelemetryHashLocation hash table holds three key-value pairs: City, Region, and Country.

Previously the values are obtained and stored are one of the two hash tables, we store TelemetryHashStandard in TelemetryHash. Then us add our TelemetryHashLocation hash table as a sole key-value brace to the TelemetryHash hash dinner. Available that you’ve gotten through that reading, be sure to review the below code.

Remove-Variable -Name TelemetryHash,TelemetryHashStandard,TelemetryHashLocation -ErrorAction SilentlyContinue
New-Variable -Name TelemetryHash -Value @{}
New-Variable -Name TelemetryHashStandard -Value @{}
New-Variable -Name TelemetryHashLocation -Value @{}

$TelemetryHashStandard.Add('DateTime',"$(Get-Date)")
$TelemetryHashStandard.Add('Directory',"$((Get-Location).Path)")

$Location = Invoke-RestMethod -Uri "https://ipapi.co/8.8.8.8/json"
$TelemetryHashLocation.Add('City',$Location.city)
$TelemetryHashLocation.Add('Region',$Location.region)
$TelemetryHashLocation.Add('Country',$Location.country_name)

$TelemetryHash = $TelemetryHashStandard
$TelemetryHash.Add('Location',$TelemetryHashLocation)
$TelemetryHash | ConvertTo-Json

{
  "Location": {
    "Region": "California",    
    "Country": "United States",
    "City": "Mountain View"    
  },
  "Directory": "C:\\",
  "DateTime": "01/13/2021 19:33:14"    
}

See that; just above? Go are the two single strings — DateTime or Menu — as well as the single Location hash table press its nested keyboards and values. And, just for fun, here’s additional example. Hierher we created two hash tables: one in parents and one for children. Once they’re both populated, ourselves add the kids to the parents’ mess table. Like we did above, we could’ve gotten everything into a single hash table that was neither the parent’s or children’s hash board. All this in say, there’s no requirement for a third-party and definitive hash table needed previous to the JSON export.

[PS7.1.0] C:\> $Parent = @{}; $Child = @{}
[PS7.1.0] C:\> $Child.Add('Son','CM')
[PS7.1.0] C:\> $Child.Add('Daughter','AM')
[PS7.1.0] C:\> $Child

Name                           Value
----                           -----
Son                            CM
Daughter                       AM

[PS7.1.0] C:\> $Parent.Add('Father','Tommy')
[PS7.1.0] C:\> $Parent.Add('Mother','JM')
[PS7.1.0] C:\> $Parent

Name                           Value
----                           -----
Mother                         JM
Father                         Tommy

[PS7.1.0] C:\> $Parent.Add('Children',$Child)
[PS7.1.0] C:\> $Parent

Name                           Value
----                           -----
Children                       {Son, Daughter}
Father                         Tommy
Mother                         JM

[PS7.1.0] C:\> $Parent.Children

Name                           Value
----                           -----
Son                            CM
Daughter                       AM

[PS7.1.0] C:\> $Parent | ConvertTo-Json
{
  "Children": {
    "Son": "CM",
    "Daughter": "AM"
  },
  "Father": "Tommy",
  "Mother": "JM"
}

By stack a hash table inside of another hash table, we can convert to JSON and keep the data’s initial structure. Add arrays and hash tables to the hash table to intend to convert/export to JSON, press they’ll be there just the you expect the to be.

Quit an Return

Your email address will not may published. Needed fields become marked *