Convert string to objects?

Welcome Forums General PowerShell Q&A Convert string to objects?

This topic contains 6 replies, has 5 voices, and was last updated by

1 year, 4 months ago.

  • Author
  • #74506

    Points: 0
    Rank: Member

    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?

  • #74509

    Points: 0
    Rank: Member

    Output the values to pscustomobject

    • #74537

      Points: 0
      Rank: Member
      $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?:

  • #74545

    Points: 17
    Rank: Member
    $uri = ''
    $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]
            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

      Points: 0
      Rank: Member

      Thanks. Looks like where on to something.

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

      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

    Points: 0
    Rank: Member

    Check this tool: json formatter
    Hope this help!

  • #80098

    Points: 0
    Rank: Member

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

    # Read data from URI
    $uri = ''
    $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) {
    # 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)
        Write-Host " "
    # Convert the object to JSON
    $json_Object = $final_object_list | ConvertTo-Json 

The topic ‘Convert string to objects?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort