JSON manipulation using Powershell

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

JSON manipulation using Powershell

Post by rslaav »

I am trying to grab data from a Json file, and then output only the values associated to objects that have a field that reads "pass". I am not exactly sure how to proceed, as I've not touched powershell in a while now, and mostly been working on linux. I know it's probably just a simple command, but here is what I have so far:

$json = Get-Content 'file.json' | Out-String | ConvertFrom-Json
$output = $json.tests.results

I end up getting a whole bunch of stuff with results "bad", "warning", "pass", "fail". I only want to show the ones tagged as "pass"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: JSON manipulation using Powershell

Post by jvierra »

Again, not enough information to advise you on how to obtain the results desired. This is mostly because you have no PowerShell experience at teh level needed to understand the results or how to inspect objects and use the default CmdLets to filter objects/object collections.

Start with: "'help Select-Object', and 'help Where-Object'"
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: JSON manipulation using Powershell

Post by rslaav »

I appreciate the reply, but maybe try to stop saying "you don't have enough experience" as though you're the only person who understands how to be a programmer.

I know the question sounds vague, but really I was trying to avoid pasting a bunch of json data that has sensitive info. Mainly I get results that I understand, but each column in the result has all the values I need and don't need.

For instance:
  1. $output | Get-Members
  2.  
  3. ID                                                 Result
  4. Whatever                                     Bad
  5. Something-Else                            Warning
  6. Another-thing                              Pass
  7. Blah                                            Fail
  8. And-Another                               Pass

I just want the "pass results" to be showing, and the rest to be filtered out:
  1. ID                                               Result
  2. Another-Thing                            Pass
  3. And-Another                               Pass
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: JSON manipulation using Powershell

Post by jvierra »

rslaav wrote: Wed Feb 23, 2022 6:43 pm I appreciate the reply, but maybe try to stop saying "you don't have enough experience" as though you're the only person who understands how to be a programmer.
Sorry but I have at least 40 years more experience and your question show you have little if any real programming experience. Simple coding is not programming beyond writing simple code. My statement was to help you think that maybe a book or other formal; method would help you to ask better technical questions. With very little effort you could ask better questions.

I will try to decode what you have posted. In technical forums we usually try not to male others have to decode our questions. I will try. Maybe I will guess right but maybe I can't.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: JSON manipulation using Powershell

Post by jvierra »

The best guess is that you should filter on: "$_.Results -eq 'Pass'". Again a basic book on PowerShell would have shown you thins within two chapters.

Programmers are required to study any language they use to a fairly deep level. Writing a few lines of code is not programming. Programming is a tough technical discipline. PowerShell is designed for non-coders if you just want to write batch files.

Also use the PS help system to learn about the basic commands and how to use them.

I am not picking on you. This is advice given to all who enter technology at any level. If you want I can suggest books and other methods that can help you learn PowerShell and formal programming.
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: JSON manipulation using Powershell

Post by rslaav »

You're pretty stupid for someone with 40 years of experience. Maybe you've just been repeating the same year forty times. It's a common problem with people who suffer from myopia. You could have also just started with "If you want I can suggest books and other methods that can help you learn PowerShell".

Most of my work has been in Java, and C/C++ on Linux, working on encryption. No programming to do with windows, until recently. Don't worry about the help, I've figured it out myself. Dipstick.
User avatar
Alexander Riedel
Posts: 8472
Last visit: Mon Mar 18, 2024 2:59 pm
Answers: 19
Been upvoted: 37 times

Re: JSON manipulation using Powershell

Post by Alexander Riedel »

Guys. Please keep it professional and courteous.
Alexander Riedel
SAPIEN Technologies, Inc.
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: JSON manipulation using Powershell

Post by rslaav »

Image
This topic is 2 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked