2

I'm attempting to port .Net PowerShell code over to Python. In order to ensure that the python code is correct I'm testing the PowerShell output vs the Python output.

In powershell I have

$ps_output = PowerShell-Function $log_path
$python_output = Python-Function $log_path
($ps_output -eq $python_output)

and if I run compare-object I get

InputObject                                                                                                 SideIndicator                                                                                              
-----------                                                                                                 -------------                                                                                              
The Python Win32 extensions for NT (service, event logging) appear not to be available.                     =>                                                                                                         
[{'blocks': '5860533168', 'sn': 'PN2234J4T5DR', 'fw': 'MF8OAC0', 'device': 'da1', 'data': ['@{n... =>                                                                                                         
[{"bay":"1","device":"da1","make":"HGST","type":"HUS724030ALA640","fw":"MF8OAC0","sn":"PN2234J4T5DR",... <=  

I'm reasonably sure that the reason the equality check is failing is because the ordering of the key value pairs is different from python to PowerShell.

So I'm looking for a way to test equality between json objects in powershell or a means to order my python produced json in a way that matches the powershell output.

I've tried using an OrderedDict. This has the two fold problem of putting

[OrderedDict{...

in front of the string and also makes the code pretty brittle (since I'll have a dependency on when something is added to a dictionary).

AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103

1 Answers1

4

Do you try to first convert to a PowerShell Object using ConvertFrom-Json and then use Compare-Object ?

$ps_output = PowerShell-Function $log_path
$python_output = Python-Function $log_path
$po_ps_output = $ps_output | ConvertFrom-Json
$po_python_output = $python_output | ConvertFrom-Json
Compare-Object $po_ps_output $po_python_output
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • So I've run into a major flaw with ConvertFrom-Json. It doesn't work if the JSON is larger than 1MB. – AlexLordThorsen Jun 06 '14 at 18:22
  • Don't you have a trouble with the depth see [Convertto-Json](http://stackoverflow.com/a/17972159/608772) (The same to ConvertFrom-Json).Perhaps you can convert JSON to XML have a look to [Convert-JsonToXml](http://stackoverflow.com/a/22131452/608772) – JPBlanc Jun 06 '14 at 18:30
  • Ahhh, derp. that fixes the depth issue but my python didn't produce proper json. I'll make this answer as correct after I have the code actually working. – AlexLordThorsen Jun 07 '14 at 00:00