Hello, I’m using the HSLJson library to parse a json string storing relevant run information and reformatting it to the proper output format. In this case, my JSON is formatted as such:
{
“EquipmentID”: “SN0001”,
“Operator”: “Sam”,
“StartTime”: “3:00”,
“EndTime”: “4:00”
}
I’d like to get an array containing the JSON keys so that I can loop through each one, grabbing the value so I can write the output. It would be simple enough if I knew the keys in advance but I’m trying to make this function as flexible as possible so that it can process any combination of keys. Is there a straightforward way to get the keys from a JSON object in Venus that I’m missing?
Thanks,
Philip
EDIT:
I ended up bootstrapping this functionality using Powershell and the Shell function in Venus but I’d still be interested in learning if there’s a native Venus way to do this.
Hi @pthatcher ,
The HSLJson library is a custom library. It appears that the original author had not included a GetKeys functions unfortunately. There is a GetJson function that will return the JSON object as a string. You could include logic to parse the string to eventually get the Key values.
Unfortunately, this is limited to simple JSON files like what you listed as an example.
TraceArray - progress; Values [1]:
TraceArray - progress; Values [2]: "EquipmentID":"SN0001","Operator":"Sam","StartTime":"3:00","EndTime":"4:00"}
TraceArray - complete; ----------------------------------------------------------------------------------------
TraceArray - start; ----------------------------------
TraceArray - progress; Values [1]: "EquipmentID":"SN0001"
TraceArray - progress; Values [2]: "Operator":"Sam"
TraceArray - progress; Values [3]: "StartTime":"3:00"
TraceArray - progress; Values [4]: "EndTime":"4:00"
TraceArray - complete; ----------------------------------
TraceArray - start; ---------------------
TraceArray - progress; Keys [1]: EquipmentID
TraceArray - progress; Keys [2]: Operator
TraceArray - progress; Keys [3]: StartTime
TraceArray - progress; Keys [4]: EndTime
TraceArray - complete; ---------------------
Thanks Brandon, I figured this was a missing function. Thanks for providing your venus solution. I’ll provide my powershell implementation here for posterity.
JSON_Get_Keys.ps1:
param (
[Parameter(Mandatory)]
[string]$JsonInput
)
# Initialize success flag
$success = $true
# Define the path for the output file (same directory as input file)
$outputFilePath = Join-Path (Split-Path -Parent $JsonInput) 'temp_json_keys.txt'
try {
# Check if input is a file
if (Test-Path $JsonInput) {
# Read the content of the file
$JsonString = Get-Content -Path $JsonInput -Raw
} else {
# If it's not a file, treat it as a JSON string directly
$JsonString = $JsonInput
}
# Print the received JSON string for debugging
Write-Host "Received JSON String:" -ForegroundColor Yellow
Write-Host $JsonString -ForegroundColor Green
# Convert JSON string to PowerShell object
$jsonObject = $JsonString | ConvertFrom-Json
# Retrieve keys
$keys = $jsonObject.PSObject.Properties.Name
# Display the keys
Write-Host "JSON Keys:" -ForegroundColor Yellow
$keys
# Write the keys to the output file, each key on a new line, using ASCII encoding
$keys | Out-File -FilePath $outputFilePath -Force -Encoding ASCII
Write-Host "Keys have been written to: $outputFilePath" -ForegroundColor Green
} catch {
# Error handling
$errorMessage = "Error parsing JSON: $_"
# Write the error to the output file
$errorMessage | Out-File -FilePath $outputFilePath -Force -Encoding ASCII
Write-Error $errorMessage
# Set success flag to false if there was an error
$success = $false
}
# Pause at the end (for debugging)
# Pause
# Conditional exit based on success flag
if ($success) {
# If no errors, exit with code 0
exit 0
} else {
# If there was an error, exit with code 1
exit 1
}
1 Like