Powershell: Support Question: Unable to extract data from JSON file

Created on 21 Nov 2019  路  7Comments  路  Source: PowerShell/PowerShell

Support Question

I've got a JSON file in this format:
[
{
"SensorApp": "HWiNFO",
"SensorClass": "System: DELL XPS 13 9380",
"SensorName": "Virtual Memory Commited",
"SensorValue": "7286",
"SensorUnit": "MB",
"SensorUpdateTime": 1574031681
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System: DELL XPS 13 9380",
"SensorName": "Virtual Memory Available",
"SensorValue": "1926",
"SensorUnit": "MB",
"SensorUpdateTime": 1574031681
},
{
"SensorApp": "HWiNFO",
"SensorClass": "System: DELL XPS 13 9380",
"SensorName": "Virtual Memory Load",
"SensorValue": "79",
"SensorUnit": "%",
"SensorUpdateTime": 1574031681
}
]

Yet every time I convertfrom-JSON and try to get data out (In this case, trying to get temperature info from HWiNFO64), it returns as empty. I can format as a table and see all the data, however nothing I do seems to be able to parse the content of the file and return a value.

So my question here is how do I go about getting information (Say in this case, I want to find anything with sensorname -like tjmax and then return the sensorvalue from that object)

Any suggestions welcome. For reference I'd consider myself competent but not an expert. I'm really hoping there's an easy obvious way to do this that I just missed.

192.168.66.38.txt

Official support

PowerShell Support Lifecycle

Community Resources

Slack and Discord Community Chat - Interactive chat with other PowerShell enthusiasts. Both Slack and Discord are bridged via a bot and can seamlessly talk to each other.

StackOverflow.com and PowerShell.org Forum - Search or post new general PowerShell usage questions

Issue-Question Resolution-Answered

All 7 comments

Generally, please note that the issue template is used isn't actually meant to create an issue with - instead, its purpose is to provide _information_ as to _where else_ you can ask for support.

@SteveL-MSFT, can we try to make this clearer in the various information-only templates? Along the lines of a conspicuous preamble along the lines of:

PLEASE DO NOT CREATE AN ISSUE HERE - just read the information below and take the appropriate action elsewhere. Use the Preview tab for a better reading experience and clickable links.

As for your specific question:

You can use the following to select all objects that have a nonempty .SensorName property:

((Get-Content -raw file.json) | ConvertFrom-Json) | Where-Object SensorName

To extract just the .SensorName property values (you'll get an error for input objects that don't have this property, which you can suppress):

((Get-Content -raw file.json) | ConvertFrom-Json) | Select-Object -ExpandProperty SensorName

Note the (...) around the ConvertFrom-Json call, which is an obscure workaround that is currently required in order to _enumerate_ the objects contained in the JSON array, i.e. to send them _one by one_ through the pipeline, as you would expect.

The need for this workaround is about to go away - see #3424

@mklement0 I wonder if we should just remove that template and instead put Support higher in the main README.md

@SteveL-MSFT: That would have to been done for all info-only templates, though:

  • Documentation Issue
  • Security Issue
  • Support Question
  • Windows PowerShell

Actually, I just noticed the last entry on the new-issue landing page, "Report a security vulnerability", which isn't a template, but simply links to another page (https://github.com/MicrosoftDocs/PowerShell-Docs/security/policy), which looks like the right approach for two reasons:

  • It's read-only - no temptation to submit an issue.
  • It renders readably and with clickable links by default.

Therefore, can't we use the same approach for all the info-only templates above?

GitHub
GitHub is where people build software. More than 40 million people use GitHub to discover, fork, and contribute to over 100 million projects.

@mklement0 yes, that makes the most sense. I'll submit a PR.

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings