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).

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

So I've run into a major flaw with ConvertFrom-Json. It doesn't work if the JSON is larger than 1MB.
Don't you have a trouble with the depth see Convertto-Json (The same to ConvertFrom-Json).Perhaps you can convert JSON to XML have a look to Convert-JsonToXml
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.