Evaluating json array objects. Please assist

Welcome Forums General PowerShell Q&A Evaluating json array objects. Please assist

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #168463

    Participant
    Topics: 4
    Replies: 8
    Points: 16
    Rank: Member

    I need to evaluate if current month and day is true based on start and end in each array object and if it is then print the cns value. Thank you for your help!

    #json file

    {
       "times": [{ "start": "01/01", "end": "03/01", "cns": "8" }, { "start": "04/01", "end": "07/01", "cns": "6" } ]
    }

     

  • #168478

    Participant
    Topics: 0
    Replies: 15
    Points: 61
    Helping Hand
    Rank: Member

    Hi, I don't know how you are going to read this. Assuming, you are reading it from a file.

    $x = Get-Content C:\Temp\json.txt | ConvertFrom-Json
    PS M:\> $x.times
    
    start end   cns
    —– —   —
    01/01 03/01 8  
    04/01 07/01 6
    
    

    After the 'ConvertFrom-Json' command, you will get a PSCustomObject and you can iterate through each item.

  • #168559

    Participant
    Topics: 4
    Replies: 8
    Points: 16
    Rank: Member

    Thank you for your reply! How to iterate through each item?

    Thanks!

     

  • #168616

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    Please start with learning the very basics of Powershell. It's beyond the scope of any forum to teach you the fundamentals of any technology. You will have to learn that first.

    You can use Foreach-Object or about_foreach.

  • #168640

    Participant
    Topics: 4
    Replies: 8
    Points: 16
    Rank: Member

    Understand. Thank you for your time!

You must be logged in to reply to this topic.