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模块是实现序列化和反序列话功能的''' ...
随机推荐
- vue项目中实现图片懒加载的方法
对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载.这样子对于页面加载性能上会有很大的提升,也提高了用户体验. 实 ...
- Windows地址栏的妙用
主角: 它就是windows自带的一个小工具->地址栏,可以通过在任务栏右键选择工具栏-地址栏添加使用. 妙用: 一.打开文件 使用方法:D:\Temp(文件路径) 小提示:快速进入回收站:Re ...
- Java学习笔记【四、类、对象、接口】
编程语言的发展 机器语言 过程语言 面向对象编程--封装.继承.多态 关键字 extends implements override overload super this static abstra ...
- tomcat启动程序乱码和tomcat启动程序的标题乱码处理
启动程序运行中的文字乱码: 解决方案: 找到Tomcat目录下conf文件夹中的logging.properties文件, 打开logging.properties文件,找到文件中的java.util ...
- 关于api接口以及页面数据通信域名,缓存cdn设置优化
以B站为例: 主域名:www.bilibili.com 账户登录注册相关域名,包括app登录:passport.bilibili.com 静态资源css,js,img等相关域名:static.bili ...
- centos 7 源代码搭建部署 zabbix-4.0.13 LTS
Zabbix 官网 >:https://www.zabbix.com/download 源代码地址>:https://www.zabbix.com/cn/download_sources# ...
- JDBC概述及JDBC完成对Oracle的增删改查
什么是JDBC JDBC(Java Data Base Connectivity,Java数据库连接),是一种用于执行SQL语句的Java API,为多种关系数据库提供统一访问.它由一组用Java语言 ...
- redis整合Spring入门
首先 衷心感谢这篇博客给我入门时的启发 三颗心脏 你需要知道,spring的官方文档中已经注明,与redis整合时,spring的jar包版本不能低于4.2.6,否则不支持,会报错的哟 测试的时候请 ...
- Linux中退出编辑模式的命令
vim 有三种模式,注意:这三种模式有很多不同的叫法,我这里是按照鸟哥的linux书中的叫法. 一般指令模式.编辑模式.指令列命令模式 1.vim 文件名 进入一般模式: 2.按 i 进行编 ...
- 第二章 Vue快速入门--9 使用v-on指令定义Vue中的事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...