Issue
I've spent all day trying to figure this out, any help would be great!
This is the text I want to parse into Json
:
============ Get Mouse Position ===============
Get Mouse Position
GetMousePos
Get the mouse position first
Command one
Command two
Command three
============ Set Mouse Position ===============
Set Mouse Position
SetMousePos
Set the mouse position after
Command one
Command two
Command three
The Json
output I am aiming for is:
{
"Get Mouse Position": {
"Trigger": "GetMousePos",
"Description": "Get the mouse position first",
"body": [
"Command one",
"Command two",
"Command three"
]
},
"Set Mouse Position": {
"Trigger": "SetMousePos",
"Description": "Set the mouse position after",
"body": [
"Command one",
"Command two",
"Command three"
]
},
}
I've been doing allot of reading and experimenting but I just cant seem to figure it out. the ConvertFrom-StringData command gets me mostly there but it does not seem to support arrays for a single key (for command one, two, three section).
I tried to experimenting with PSCustomObject
and ConvertTo-Json
like below and it gives me perfect Json
output but I cant wrap my head around how to properly code my data into the example below for every potential ===== [Object] ====
section there could be.
$myObject = [PSCustomObject]@{
'Get Mouse Position' = [ordered]@{
Trigger = 'GetMousePos'
Description = 'Get the mouse position first'
body = @(
'Command one'
'Command two'
'Command three'
)
}
'Set Mouse Position' = [ordered]@{
Trigger = 'Set MousePos'
Description = 'Set the mouse position last'
body = @(
'Command one'
'Command two'
'Command three'
)
}
}
I would really like to know some potential ways I can approach this, that would be so wonderful. Also, a related question. Does the ConvertFrom-StringData command not support multiple values/arrays for a single key? Thanks allot
PS: I have taken the liberty to also ask this question in other communities.
Solution
# Initialize an ordered hashtable.
$oht = [ordered] @{}
# Split the input file into blocks of lines, not including the header
# (separator) lines.
(Get-Content -Raw file.txt) -split '(?m)^===.*\r?\n' -ne '' | ForEach-Object {
# Parse the block of lines into individual lines
$name, $trigger, $description, $body = $_ -replace '\r?\n\z' -split '\r?\n'
# Add an entry to the ordered hashtable.
$oht[$name.Trim()] = [ordered] @{
Trigger = $trigger.Trim()
Description = $description.Trim()
body = $body.Trim()
}
}
# Convert the resulting ordered hashtable to JSON
$oht | ConvertTo-Json
Note: The above uses a nested [ordered]
hashtable instead of a nested [pscustomobject]
, as that is sufficient for to-JSON conversion and makes iterative construction easier (while also being more light-weight).
If a nested [pscustomobject]
instance is desired, replace [ordered]
inside the ForEach-Object
script block with [pscustomobject]
for the nested instance, and then use [pscustomobject] $oht
to construct the outer instance.
See also:
-split
, the regular-expression-based string-splitting operator-replace
, the regular-expression-based string-replacement operator
Answered By - mklement0 Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.