Convert string to objects?

This topic contains 6 replies, has 5 voices, and was last updated by  manjunathrao25 1 month ago.

  • Author
    Posts
  • #74506

    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

    Ben Camareno
    Participant

    Output the values to pscustomobject

    • #74537

      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

    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

      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?

  • #79468

    James
    Participant

    Check this tool: json formatter
    Hope this help!

  • #80098

    manjunathrao25
    Participant

    Hello Alexander,
    Please use the below code and let me know if it works:

    
    # Read data from URI
    $uri = 'http://www.officeholidays.com/countries/albania/index.php'
    $html = Invoke-WebRequest -Uri $uri
    
    # Get the column header and save it into a list - $header_list
    $header_table = $html.ParsedHtml.getElementsByTagName('TR')
    $data = $header_table[0].cells
    $header_list = New-Object System.Collections.ArrayList
    
    foreach($i in $data) {
        $i.outerText
        $header_list.add($i.outerText)
    }
    
    # Get the holiday data
    $data_table = $html.ParsedHtml.getElementsByTagName('TR') | where {$_.classname -eq 'holiday'}
    $final_object_list = New-Object System.Collections.ArrayList
    
    foreach($i in $data_table) {
    $header_count = 0
        $single_row_entry = $i.cells
        $object = New-Object System.Object
        foreach ($j in $single_row_entry){
            $value = $j.outertext
    
            if ($j.outerText -eq $null){
                $value = " "
            }
            $object | Add-Member –MemberType NoteProperty –Name $header_list[$header_count++] –Value $value
        }
        # Print each object that we are creating
        write-host ($object | format-list | out-string)
        $final_object_list.Add($object)
        Write-Host " "
    }
    
    # Convert the object to JSON
    $json_Object = $final_object_list | ConvertTo-Json 
    
    

You must be logged in to reply to this topic.