Powershell

What is ConvertFrom-Json (Microsoft.PowerShell.Utility)?

The “ConvertFrom-Json” is a command-line tool in PowerShell that shifts a string to a custom object or a hash table. JSON (JavaScript Object Notation) is a lightweight data exchange protocol. It creates an easily understandable code for humans. Moreover, it can also be used to read, write, parse, and generate code.

In this post, the “ConvertFrom-Json” cmdlet will be explained in detail.

What is ConvertFrom-Json (Microsoft.PowerShell.Utility)?

JSON is most commonly utilized by websites to give a visual representation of objects or a hash table. Let’s overview some of the examples explaining the stated cmdlet.

Example 1: Convert a DateTime Object to a JSON Object

Execute the below-given command to convert “DateTime” to a JSON object:

Get-Date | Select-Object -Property * | ConvertTo-Json | ConvertFrom-Json

 

Following the above-stated code:

  • First, specify the “Get-Date” cmdlet along with the “|” pipeline.
  • Then, write the “Select-Object” to select a property along with the “-Property” parameter and the “*” wildcard.
  • After that, add another “|” pipeline alongside the “ConvertTo-Json” cmdlet and the pipeline “|”.
  • Lastly, mention the “ConvertFrom-Json” cmdlet:

Example 2: Convert a JSON String to a Hash Table

To convert a JSON string to a hash table, run the below code:

'{ "key":"val_1", "Key":"val_2" }' | ConvertFrom-Json -AsHashtable

 

In the above-stated code:

  • First, write a hash table having the specified values.
  • Then, use the “|” pipeline and add the “ConvertFrom-Json” cmdlet.
  • Lastly, mention the “-AsHashtable” parameter at the end:

That was all about the “ConvertFrom-Json” cmdlet in PowerShell.

Conclusion

The “ConvertFrom-Json” cmdlet in PowerShell converts JSON “JavaScript Object Notation” formatted string to a custom object or a hashtable. It reads a raw “JSON” string and converts it into an object. This write-up has explained the “ConvertFrom-Json” cmdlet in detail.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.