-->

dev powershell

Sometimes I’m amazed by how some tasks are easy to carry out using PowerShell. One such task is converting common data types such as CSV, JSON and XML. PowerShell has built-in support for all these types.

Convert CSV to JSON

For example, let’s say we have the following customer data in a CSV file named customers.csv

Id,FirstName,LastName,Country,Status
1,James,Monroe,USA,Active
2,Diane,Wheatley,UK,Active
3,Sara,Bailey,UK,Suspended

If we want to convert this data into JSON, we can run the following command in PowerShell:

Import-Csv ./customers.csv | ConvertTo-Json | Out-File ./customers.json

we get the following JSON output:

[
  {
    "Id": "1",
    "FirstName": "James",
    "LastName": "Monroe",
    "Country": "USA",
    "Status": "Active"
  },
  {
    "Id": "2",
    "FirstName": "Diane",
    "LastName": "Wheatley",
    "Country": "UK",
    "Status": "Active"
  },
  {
    "Id": "3",
    "FirstName": "Sara",
    "LastName": "Bailey",
    "Country": "UK",
    "Status": "Suspended"
  }
]

The output JSON is indented by default, so very easy to read as well.

Convert JSON to CSV

If we want to reverse the process and create a CSV file from a JSON input, we can run the following:

(Get-Content -Path ./customers.json -Raw | ConvertFrom-Json) | Export-CSV ./customers.csv

and get a CSV that looks like:

"Id","FirstName","LastName","Country","Status"
"1","James","Monroe","USA","Active"
"2","Diane","Wheatley","UK","Active"
"3","Sara","Bailey","UK","Suspended"

I used the Export-CSV cmdlet in this example because it saves the output to a file. We can also use ConvertTo-Csv cmdlet

For example, the following snippet print the results to the console:

(Get-Content -Path ./customers.json -Raw | ConvertFrom-Json) | ConvertTo-Csv | Write-Host

Convert JSON to XML

Similar to JSON, we can use the ConvertTo-XML cmdlet to create an XML file out of our sample JSON:

Export-Clixml -Depth 3 -InputObject ((Get-Content -Path ./customers.json -Raw) | ConvertFrom-Json) -Path ./customers.xml

and the output looks like this:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Object[]</T>
      <T>System.Array</T>
      <T>System.Object</T>
    </TN>
    <LST>
      <Obj RefId="1">
        <TN RefId="1">
          <T>System.Management.Automation.PSCustomObject</T>
          <T>System.Object</T>
        </TN>
        <MS>
          <S N="Id">1</S>
          <S N="FirstName">James</S>
          <S N="LastName">Monroe</S>
          <S N="Country">USA</S>
          <S N="Status">Active</S>
        </MS>
      </Obj>
      <Obj RefId="2">
        <TNRef RefId="1" />
        <MS>
          <S N="Id">2</S>
          <S N="FirstName">Diane</S>
          <S N="LastName">Wheatley</S>
          <S N="Country">UK</S>
          <S N="Status">Active</S>
        </MS>
      </Obj>
      <Obj RefId="3">
        <TNRef RefId="1" />
        <MS>
          <S N="Id">3</S>
          <S N="FirstName">Sara</S>
          <S N="LastName">Bailey</S>
          <S N="Country">UK</S>
          <S N="Status">Suspended</S>
        </MS>
      </Obj>
    </LST>
  </Obj>
</Objs>

A bit noisy, but the data is there.

Conclusion

Using PowerShell’s built-in cmdlets, we can easily convert between common data types such as CSV, JSON and XML. For a complete list of supported cmdlets, please check the link in the resources section.

Resources

dev dotnet

I like LINQPad for prototyping C# applications and trying out short snippets. In many scenarios, I have to see the output of what I’m trying out. I used to treat my snippets as small console applications, and I used to use Console.WriteLine() statements to display the output.

Not anymore! In a tech video on YouTube, I saw this option and loved it:

In LINQPad, there’s a generic extension method called Dump(). It writes the output to the console. Exactly as Console.WriteLine but in a much more concise way.

For example:

var nums = new[] { 1, 2, 3, 4, 5, 6 };
var sum = nums.Aggregate((a,b) => a + b);
Console.WriteLine(sum);

The code block above displays 21, and it can be shortened with the Dump method

var nums = new[] { 1, 2, 3, 4, 5, 6 };
nums.Aggregate((a,b) => a + b).Dump();

It shows the same result but in a shorter way.

Resources

I think the best option for a blog is a static website. I love blogging using Jekyll for that reason. Just write your posts in markdown and create a static website out of it so you can host everywhere for free or at a very affordable price.

Jekyll logo

So far, my local development environment for my blog was a MacBook Pro, and everything was installed on bare metal. By everything, I mean Jekyll and all its dependencies, such as Ruby. Unfortunately, I’m not a Ruby developer, and when something breaks, it’s hard to figure out how to fix it.

You might think once you set it up, you don’t need to change much. It’s true in general, except for macOS updates. However, after almost every major OS upgrade in recent years, I found out my blogging environment was broken. To mitigate this, I decided to use Docker.

Docker logo

Create your blog

I assume you don’t already have a Jekyll blog in this post. Creating a new one is very easy, as shown on the official Jekyll website:

gem install bundler jekyll
jekyll new my-awesome-site
cd my-awesome-site

Since we don’t want to install anything on our system, we are not going to use the first command and go straight to creating the site from within a Docker container like this:

docker run --rm -v "${PWD}:/srv/jekyll" jekyll/jekyll /bin/bash -c "jekyll new my-awesome-site"
cd my-awesome-site

We only have to do this once to initialize our blog. After that, each time we want to run it locally for testing purposes, we can run this:

docker run --rm -v "${PWD}:/srv/jekyll" -p 4000:4000 -it jekyll/jekyll /bin/bash -c "jekyll serve --drafts --trace"

Notice the current directory is mounted, so make sure you are in the correct directory (which is the root of your blog) before running this command.

And that’s all there is to it!. Just navigate to 127.0.0.1:4000 on your machine to confirm it’s working and get cranking.

Happy blogging!

Conclusion

The moral of the story is: My biggest takeaway from all the containerized application movement is Never install anything on bare metal! Ever again! Whenever my development environment broke because of an unrelated change in the OS, I regretted not doing this earlier. No more!

Even though this blog itself is currently on WordPress, it’s not hard to migrate to Jekyll. I’ll get it up and running in minutes using the Docker images if that happens.

Resources