Search code examples
powershell

How to create string of foreach results


I have an if statement inside a foreach inside a foreach. Result is this a list of numbers one in a line. Example:

3
2824
28
6374
3887
198
1953
3827

I'd like to have this as a result in a variable instead:

"watchers": ["3", "2824", "28", "6374", "3887", "198", "1953", "3827"],

I currently have no real idea how to convert the foreach results to the example string. Powershell preferred but I have access to pretty much any language.

Edit: Rephrasing my question, and/or to be more specific, I have this CSV

user_id,ticket_id
253,24704
1257,24201
1257,17038
1257,22560
1257,22547
1925,24783
2452,24766
2452,24787
2453,24766
2453,24787

I'm trying to write a script that creates this JSON for me:

[{
“ticket_id”: “253”,
“watchers”: [“24704”]
}, {
“ticket_id”: “1257”,
“watchers”: [“24201”, “17038”,“22560”,“22547”]
}, {
“ticket_id”: “1925”,
“watchers”: [“24783”]
}, {
“ticket_id”: “2452”,
“watchers”: [“24766”,“24787”]
}, {
“ticket_id”: “2453”,
“watchers”: [“24766”,“24787”]
}
]

Solution

  • What you seem to be after is a Json string with a property called watchers whose value is an array of numbers, in which case constructing it manually is just wrong, you can use ConvertTo-Json, example:

    $someNumbers = 0..10
    $filteredNumbers = foreach ($number in $someNumbers) {
        if ($number -gt 5) {
            $number.ToString()
        }
    }
    ConvertTo-Json @{ watchers = $filteredNumbers } -Compress
    
    # Outputs: {"watchers":["6","7","8","9","10"]}
    

    If you wanted something exactly like the output you have in question and want to construct the string manually, you can first join the array of numbers with a delimiter ", " and then interpolate that string in a new string:

    $numbersString = $filteredNumbers -join '", "'
    '"watchers": ["{0}"],' -f $numbersString
    
    # Outputs: "watchers": ["6", "7", "8", "9", "10"],
    

    Based on comments and looking at the last edit on your question, assuming the CSV is in a file, you can use Group-Object on user_id:

    Import-Csv path\to\thefile.csv |
        Group-Object user_id |
        ForEach-Object {
            [pscustomobject]@{
                ticket_id = $_.Name
                watchers = @($_.Group.ticket_id)
            }
        } | ConvertTo-Json
    

    The output from the above using the sample CSV would be:

    [
      {
        "ticket_id": "1257",
        "watchers": [
          "24201",
          "17038",
          "22560",
          "22547"
        ]
      },
      {
        "ticket_id": "1925",
        "watchers": [
          "24783"
        ]
      },
      {
        "ticket_id": "2452",
        "watchers": [
          "24766",
          "24787"
        ]
      },
      {
        "ticket_id": "2453",
        "watchers": [
          "24766",
          "24787"
        ]
      },
      {
        "ticket_id": "253",
        "watchers": [
          "24704"
        ]
      }
    ]