Home > other >  What is the best way to interpret a Python string of a dictionary in powershell?
What is the best way to interpret a Python string of a dictionary in powershell?

Time:03-02

My input is the following string, describing a Python dict (of dicts), and I would like to convert it into a powershell hashtable. How should I proceed?

{'Animals': {'Canine': ['Dog', 'Wolf'], 'Feline': 'Cat'}, 'Fruits': {'Good':['Apple','Orange'], 'Bad': ['Durian']}}

Is there a library out there that could get this as a string input and return a hashtable object? Thanks!

CodePudding user response:

powershell have the ability to convert items from json to psobjects.

powershell 7 have added the ability to even convert it to hashtable:

"{'Animals': {'Canine': ['Dog', 'Wolf'], 'Feline': 'Cat'}, 'Fruits': {'Good':['Apple','Orange'], 'Bad': ['Durian']}}"|ConvertFrom-Json -AsHashtable
  • Related