如何在powershell中比较JSON [英] How to compare JSON in powershell

查看:79
本文介绍了如何在powershell中比较JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,我需要将文件中的 JSON 对象与进入 Anypoint MQ 队列的 JSON 消息进行比较.我能够从队列中获取消息.我使用了下面的脚本,但它不起作用.我做了 -eqCompare-Object 但它们不起作用.

I have a requirement where I need to compare JSON object from a file to the JSON message which comes into Anypoint MQ queue. I am able to get the message from the queue. I have used below script but it is not working. I did both -eq and Compare-Object but they are not working.

$po_ps_output = $filemessagecontent | ConvertFrom-Json
$po_python_output = $mqmessagecontent.body | ConvertFrom-Json
$result = $po_ps_output -eq $po_python_output

推荐答案

如果你只想知道 if 两个 JSON 来源的对象不同,而不需要知道如何:

If you just want to know if the two JSON-originated objects differ, without needing to know how:

$contentEqual = ($po_ps_output | ConvertTo-Json -Compress) -eq 
                ($po_python_output | ConvertTo-Json -Compress)

注意:

  • ConvertTo-Json 默认为 2 的序列化深度 - 如果您的数据更多,请使用 -Depth <n>深度嵌套以避免截断(潜在的数据丢失) - 请参阅这篇文章.

  • ConvertTo-Json defaults to a serialization depth of 2 - use -Depth <n> if your data is more deeply nested to avoid truncation (potentiall data loss) - see this post.

转换回 JSON 似乎是一个不必要的步骤,但 -Compress 将输出格式标准化为一行,没有额外的空格,这确保了格式中的偶然变化输入(如果您直接使用了输入 JSON 文本)将被忽略.

Converting back to JSON may seem like an unnecessary step, but the -Compress standardizes the output formatting to a single line with no extra whitespace, which ensures that incidental variations in formatting in the input (if you had used the input JSON text directly) are ignored.

如果您想知道这两个源自 JSON 的对象有何不同:

If you want to know how the two JSON-originated objects differ:

注意:以下内容仅在以下情况下有用,有限 -通用、强大的解决方案需要付出更多努力:

Note: The following is only useful in the following, limited scenario - a generic, robust solution would require much more effort:

  • 输入具有相同的结构,仅在属性名称/值上有所不同.

  • The inputs have the same structure and differ only in property names / values.

(等效)属性的顺序是相同的.

The ordering of (equivalent) properties is the same.

Compare-Object (($po_ps_output | ConvertTo-Json) -split '\r?\n') `
               (($po_python_output | ConvertTo-Json) -split '\r?\n')

输出将显示不同的行,每行代表一个属性或原始值;例如:

The output will show the lines that differ, each representing a single property or primitive value; e.g.:

InputObject                   SideIndicator
-----------                   -------------
      "DOB":  "12-03-1994"    =>
      "DOB":  "12-03-1999"    <=

注意:

  • =>/<= 表示该行对于 RHS/LHS 是唯一的.

  • => / <= indicate that the line is unique to the RHS / LHS.

再次,显式地重新转换为 JSON 以确保统一格式;在这种情况下,是一种面向行的漂亮打印格式,可实现逐个属性的比较.

Again, the explicit reconversion to JSON is done to ensure uniform formatting; in this case, a line-oriented pretty-printed format that enables property-by-property comparison.

同样,您可能必须使用 -Depth 来防止数据被截断.

Again, you may have to use -Depth to prevent truncation of data.

对于交互式差异检查,您可以尝试使用差异可视化工具,例如内置在 Visual Studio Code 中的工具,方法是将两个 JSON 字符串以漂亮的打印形式通过文件到
code --diff .

For interactive inspection of differences, you can try a difference-visualization tool, such as the one built into Visual Studio Code, by passing the two JSON strings in pretty-printed form via files to
code --diff <file1> <file2>.

至于你尝试了什么:

ConvertFrom-Json 创建 [pscustomobject] 实例,因此您正在比较该类型的两个实例:

ConvertFrom-Json creates [pscustomobject] instances, so you're comparing two instances of that type:

  • 如果你使用 -eq引用相等 会被测试,因为 [pscustomobject] 是一个引用类型并且不t 实现自定义相等比较.

  • If you use -eq, reference equality is tested for, because [pscustomobject] is a reference type and doesn't implement custom equality comparison.

  • 因此,$po_ps_output -eq $po_python_output 只会在 $true 两个变量指向内存中完全相同的对象 -这显然不是这里的情况,所以你总是会得到 $false.
  • Therefore, $po_ps_output -eq $po_python_output will only be $true if the two variables point to the very same object in memory - which is clearly not the case here, so you'll always get $false.

如果您使用 Compare-Object,两个实例将通过它们的 .ToString() 值进行比较.

If you use Compare-Object, the two instances are compared by their .ToString() values.

  • 从 PowerShell Core 7.0.0-preview.4 开始,遗憾的是,在 [pscustomobject] 实例上调用 .ToString() 会产生 空字符串 (''),这应该被认为是一个错误 - 请参阅 这个 GitHub 问题.

  • As of PowerShell Core 7.0.0-preview.4, regrettably, calling .ToString() on an instance of [pscustomobject] yields the empty string (''), which should be considered a bug - see this GitHub issue.

因此,Compare-Object $po_ps_output $po_python_output(无益地)认为两个实例相等并且不返回任何内容,因为默认情况下不输出相等的对象(使用 -IncludeEqual 来包含它们.

Therefore, Compare-Object $po_ps_output $po_python_output (unhelpfully) considers the two instances equal and returns nothing, since equal objects are by default not output (use -IncludeEqual to include them).

这篇关于如何在powershell中比较JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆