Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2
Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2
问题
Why do I get unexpected ConvertTo-Json
results?
And why does a round-trip ($Json | ConvertFrom-Json | ConvertTo-Json
) fail?
Meta issue
Stackoverflow has a good mechanism to prevent duplicate questions but as far as I can see there is no mechanism to prevent questions that have a duplicate cause.
Take this question as a an example: almost every week a new question
comes in with the same cause, yet it is often difficult to define it as a
duplicate because the question itself is just a slightly different.
Nevertheless, I wouldn't be surprised if this question/answer itself
ends up as a duplicate (or off-topic) but unfortunately stackoverflow
has no possibility to write an article to prevent other programmers from continuing writing questions caused by this “known” pitfall.
Duplicates
A few examples of similar questions with the same common cause:
- PowerShell ConvertTo-Json does not convert Array as expected
(yesterday) - Powershell ConvertTo-json with embedded hashtable
- powershell “ConvertTo-Json” has messed json format output
- Nested arrays and ConvertTo-Json
- Powershell ConvertTo-JSON missing nested level
- How to save a JSON object to a file using Powershell?
- Cannot convert PSCustomObjects within array back to JSON correctly
- ConvertTo-Json flattens arrays over 3 levels deep
- Add an array of objects to a PSObject at once
- Why does ConvertTo-Json drop values
- How to round-trip this JSON to PSObject and back in Powershell
- …
Different
So, were does this “self-answered” question differ from the above duplicates?
It has the common cause in the title and with that it might better prevent repeating questions due to the same cause.
Answer
ConvertTo-Json
has a -Depth
parameter:
Specifies how many levels of contained objects are included in the JSON representation.
The default value is 2.
Example
To do a full round-trip with a JSON file you need to increase the -Depth
for the ConvertTo-Json
cmdlet:
$Json | ConvertFrom-Json | ConvertTo-Json -Depth 9
TL;DR
Probably because ConvertTo-Json
terminates branches that are deeper than the default -Depth
(2) with a (.Net) full type name, programmers assume a bug or a cmdlet limitation and do not read the help or about.
Personally, I think a string with a simple ellipsis (three dots: …) at the end of the cut off branch, would have a clearer meaning (see also: Github issue: 8381)
Why?
This issue often ends up in another discussion as well: Why is the depth limited at all?
Some objects have circular references, meaning that a child object
could refer to a parent (or one of its grandparents) causing a
infinitive loop if it would be serialized to JSON.
Take for example the following hash table with a parent
property that refers to the object itself:
$Test = @{Guid = New-Guid}
$Test.Parent = $Test
If you execute: $Test | ConvertTo-Json
it will conveniently stop at a depth level of 2 by default:
{
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": {
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": {
"Guid": "a274d017-5188-4d91-b960-023c06159dcc",
"Parent": "System.Collections.Hashtable"
}
}
}
This is why it is not a good idea to automatically set the -Depth
to a large amount.
Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2的更多相关文章
- angularjs SyntaxError: Unexpected token in JSON at position 0
使用NodeJs读取json格式的文件,转换成对象时报错 :SyntaxError: Unexpected token in JSON at position 0,这个问题查了两三个小时,记录一下解决 ...
- uncaught syntaxerror unexpected token U JSON
uncaught syntaxerror unexpected token U JSON The parameter for the JSON.parse may be returning nothi ...
- oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)
LAST UPDATE: 1 Dec 15, 2016 APPLIES TO: 1 2 3 4 Oracle Database - Enterprise Edition - Versi ...
- 【转】C# 解析JSON方法总结
http://blog.csdn.net/jjhua/article/details/51438317 主要参考http://blog.csdn.NET/joyhen/article/details/ ...
- 《Python网络编程》学习笔记--使用谷歌地理编码API获取一个JSON文档
Foundations of Python Network Programing,Third Edition <python网络编程>,本书中的代码可在Github上搜索fopnp下载 本 ...
- (转载)Newtonsoft.Json使用总结
Newtonsoft.Json使用总结 初识JSON.......................................................................... ...
- Json.net 常用使用小结
using System; using System.Linq; using System.Collections.Generic; namespace microstore { public int ...
- PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素
以下是eleasticsearch返回的json資料:{"took" : 12,"timed_out" : false,"_shards" ...
- pickle和json模块
json模块 json模块是实现序列化和反序列化的,主要用户不同程序之间的数据交换,首先来看一下: dumps()序列化 import json '''json模块是实现序列化和反序列话功能的''' ...
随机推荐
- js跳转页面与打开新窗口的方法
1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...
- tensorboardX使用中 AttributeError: 'function' object has no attribute 'graph'
最近在使用tensorboardX可视化网络结构,因为tensorboardX并非pytorch原生的可视化工具,所以版本之间并不兼容 在使用的过程中会遇到,AttributeError: 'func ...
- TCP坚持定时器
窗口探查(window probe) 当接收方TCP缓冲区没有剩余空间后,在ACK中会通知发送方window=0,此时发送方就暂停发送数据.当接收方TCP缓冲区又有空间后,会再次发送一个ACK,告知其 ...
- shell脚本基础和grep文本处理工具企业应用2
shell脚本编程: 编程语言的分类: 根据运行方式 编译运行:源代码-->编译器(编译)-->程序文件 优 ...
- Jmeter (二) 参数化
一.数据 用户 参数化 1.添加 用户参数 添加——>前置处理器 ——>用户参数 2.设置 目标参数 3.变量代替 ${name} 4.线程组 设置循环次数,查看结果数中查看结果 thre ...
- ios h5 长按放大镜效果关闭
对需要禁用的div或者body设置下面样式-webkit-user-select: none;
- 数据库——Oracle(6)
1 默认值约束(默认值):对表中的某个列提前设置好默认值,当执行插入操作的时候,如果该列没有插入列值,则系统会自动的插入之前设置的默认值. 1)每个列只能插入一个默认值. 2)创建表的时候设置默认值. ...
- K nearest neighbor cs229
vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...
- 测试工具jmeter
测试工具jmeter http:压力测试 https://www.cnblogs.com/stulzq/p/8971531.html
- 微服务框架SpringCloud与Dubbo
#v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...