Convert string to objects?

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Alexander Holmeset Alexander Holmeset 2 weeks, 3 days ago.

  • Author
    Posts
  • #74506
    Profile photo of Alexander Holmeset
    Alexander Holmeset
    Participant

    Hi,
    I managed to capture text about holidays like this: Text capture

    How can I format/convert it to objects so I can use it like here, where the dates where in a json file?
    Json

  • #74509
    Profile photo of Ben Camareno
    Ben Camareno
    Participant

    Output the values to pscustomobject

    • #74537
      Profile photo of Alexander Holmeset
      Alexander Holmeset
      Participant
      $test2 = [pscustomobject]$test
      

      This converts it to a object i guess, but it is still one piece of text.
      How can i format it to have a name and date collum, with this kind of date format?:
      formating

  • #74545
    Profile photo of random commandline
    random commandline
    Participant
    $uri = 'http://www.officeholidays.com/countries/albania/index.php'
    $html = Invoke-WebRequest -Uri $uri
    $table = $html.ParsedHtml.getElementsByTagName('tr') |
    Where-Object {$_.classname -eq 'holiday'} |
    Select-Object -exp innerHTML
    
    foreach ($t in $table){
        [void]($t -match 'SPAN title="(.*?)"') ; $m1 = $Matches[1]
        [void]($t -match 'tooltip>(.*)') ; $m2 = $Matches[1]
        [void]($t -match 'remarks>(.*) ') ; $m3 = $Matches[1]
        [PSCustomObject]@{
            Date = $m1 ; Holiday = $m2
            Remarks = If ($m2 -ne $m3){$m3}}
    }
    
    # Results
    Date                       Holiday                    Remarks                  
    ----                       -------                    -------                  
    January 01                 New Year's Day                                      
    January 02                 Day after New Years Day                             
    March 14                   Summer Day                                          
    March 21                   Nevruz                     Spring Festival. Persi
    
    • #74633
      Profile photo of Alexander Holmeset
      Alexander Holmeset
      Participant

      Thanks. Looks like where on to something.

      Tested with Alabnia as country, and added DateTime to the variable and got the correct format.
      Working

      Then tested with Norway as country, and got it like this:
      Not working

      With Norway and several other countries there is also a class named publicholiday.

      Would I have to alter this script for each contry that's difrent?

You must be logged in to reply to this topic.