一、JSON 是什么?
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
-
“名称/值”对的集合(A collection of name/value pairs)。
不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 -
值的有序列表(An ordered list of values)。
在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
范例:
{
"jsonapi": { "version": "1.0" },
"errors": [
{
"code": "123",
"source": { "pointer": "/data/attributes/firstName" },
"title": "Value is too short",
"detail": "First name must contain at least three characters."
},
{
"code": "225",
"source": { "pointer": "/data/attributes/password" },
"title": "Passwords must contain a letter, number, and punctuation character.",
"detail": "The password provided is missing a punctuation character."
},
{
"code": "226",
"source": { "pointer": "/data/attributes/password" },
"title": "Password and password confirmation do not match."
}
]
}
二、jq 是什么?
- 格式化json输出
cat testjson |jq
orcat testjson |jq '.'
{
"jsonapi": {
"version": "1.0"
},
"errors": [
{
"code": "123",
"source": {
"pointer": "/data/attributes/firstName"
},
"title": "Value is too short",
"detail": "First name must contain at least three characters."
},
{
"code": "225",
"source": {
"pointer": "/data/attributes/password"
},
"title": "Passwords must contain a letter, number, and punctuation character.",
"detail": "The password provided is missing a punctuation character."
},
{
"code": "226",
"source": {
"pointer": "/data/attributes/password"
},
"title": "Password and password confirmation do not match."
}
]
}
- 打印某name
cat testjson |jq '.jsonapi'
{
"version": "1.0"
}
cat testjson |jq '.errors'
[
{
"code": "123",
"source": {
"pointer": "/data/attributes/firstName"
},
"title": "Value is too short",
"detail": "First name must contain at least three characters."
},
{
"code": "225",
"source": {
"pointer": "/data/attributes/password"
},
"title": "Passwords must contain a letter, number, and punctuation character.",
"detail": "The password provided is missing a punctuation character."
},
{
"code": "226",
"source": {
"pointer": "/data/attributes/password"
},
"title": "Password and password confirmation do not match."
}
]
- 打印数组内的某个元素
cat testjson |jq '.errors[0]'
{
"code": "123",
"source": {
"pointer": "/data/attributes/firstName"
},
"title": "Value is too short",
"detail": "First name must contain at least three characters."
}
- 打印数组下,全部元素下的某个name:key
cat testjson |jq '.errors[] |{code: .code ,title: .title ,pointer: .source.pointer}'
{
"code": "123",
"title": "Value is too short",
"pointer": "/data/attributes/firstName"
}
{
"code": "225",
"title": "Passwords must contain a letter, number, and punctuation character.",
"pointer": "/data/attributes/password"
}
{
"code": "226",
"title": "Password and password confirmation do not match.",
"pointer": "/data/attributes/password"
}
网友评论