Substituting empty String with null

Hello.

I’ve googled a bit for this and couldn’t find a really clean way of doing it. I need a way of substituting an empty string with null within a table.

I have written a very basic PS Azure function to take a base64 encoded CSV file in an HTTP request body and respond with a JSON string. The relevant part of the code is here:

# Look for a JSON object property called fileContent.

$fileContent = $Request.Body.fileContent

# If the required object property is missing, return a string response

$body = "Pass a base64 encoded file in the body with the following schema: {`"fileContent`":`"Base64 encoded CSV goes here`"}"
$contentType = "text/plain"

#If it is present, do stuff
if ($fileContent) {

  # Decode the Base64 string into text  
  $decodedCSV=  [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($fileContent))

  # Convert it to a PSObject using ConvertFrom-CSV
  $convertedObject = ConvertFrom-Csv -InputObject $decodedCSV

  # Now convert the PSObject into JSON format
  $JsonOutput = ConvertTo-Json -InputObject $convertedObject

  # Now return the output of that function
  $body = $JsonOutput
  $contentType = "application/json"
}

Now, if I pass in a CSV file that looks like this:

ReferenceNumber,ShipDate,CancelDate,Notes,ShipTo Name,ShipToCompany,ShipToAddress1,ShipToAddress2,ShipToCity,ShipToState,ShipToCountry,ShipToPhone,ShipToEmail,ShipToCustomerID,ShipToDeptNumber,RetailerID,SKU,Quantity,UseInsurance,Carrier Notes
a,a,a,a,a,a,a,a,a,a,,a,a,a,a,a,,a,a,a
b,c,,b,c,,b,c,d,b,c,d,b,c,d,b,c,d,b,

(note there are empty cells on line 2 and 3)
Then the response looks like this:


Notice the properties that were empty in the CSV have empty strings in the response. This is kind of OK for me - this function will only be triggered within a Logic App and I can transform the response in there if needs be, but it would be good if I could easily substitute those empty strings with null in the PS function.

Personally I find it easier to work with null where no value exists than an empty string in my downstream processing. If this is easy enough to do without looping or otherwise causing a slow response time I’d love to know how.

Many thanks in advance
Will.

Hi Will, welcome to the forum :wave:

Once converted to JSON, it looks like a straightforward -replace works.

Compare the following:

$data = @'
ReferenceNumber,ShipDate,CancelDate,Notes,ShipTo Name,ShipToCompany,ShipToAddress1,ShipToAddress2,ShipToCity,ShipToState,ShipToCountry,ShipToPhone,ShipToEmail,ShipToCustomerID,ShipToDeptNumber,RetailerID,SKU,Quantity,UseInsurance,Carrier Notes
a,a,a,a,a,a,a,a,a,a,,a,a,a,a,a,,a,a,a
b,c,,b,c,,b,c,d,b,c,d,b,c,d,b,c,d,b,
'@

$csv = $data | ConvertFrom-Csv
$Json = $csv | ConvertTo-Json
$results = $Json | ConvertFrom-Json
$null -eq $results[0].ShipToCountry
$null -eq $results[0].SKU
$data = @'
ReferenceNumber,ShipDate,CancelDate,Notes,ShipTo Name,ShipToCompany,ShipToAddress1,ShipToAddress2,ShipToCity,ShipToState,ShipToCountry,ShipToPhone,ShipToEmail,ShipToCustomerID,ShipToDeptNumber,RetailerID,SKU,Quantity,UseInsurance,Carrier Notes
a,a,a,a,a,a,a,a,a,a,,a,a,a,a,a,,a,a,a
b,c,,b,c,,b,c,d,b,c,d,b,c,d,b,c,d,b,
'@

$csv = $data | ConvertFrom-Csv
$Json = $csv | ConvertTo-Json
$Json = $Json -replace '""','null'
$results = $Json | ConvertFrom-Json
$null -eq $results[0].ShipToCountry
$null -eq $results[0].SKU
1 Like

Perfect, thanks. Simple string replacement in the JSON output. Not sure why I didn’t try this.

Will.