一贞岭、JSON 是什么?
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式赴背。 易于人閱讀和編寫山宾。同時也易于機器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集桦他。 JSON采用完全獨立于語言的文本格式,但是也使用了類似于C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)瓤狐。 這些特性使JSON成為理想的數(shù)據(jù)交換語言瞬铸。
JSON建構于兩種結構:
-
“名稱/值”對的集合(A collection of name/value pairs)批幌。
不同的語言中础锐,它被理解為對象(object),紀錄(record)荧缘,結構(struct)皆警,字典(dictionary),哈希表(hash table)截粗,有鍵列表(keyed list)信姓,或者關聯(lián)數(shù)組 (associative array)。 -
值的有序列表(An ordered list of values)绸罗。
在大部分語言中意推,它被理解為數(shù)組(array)。
這些都是常見的數(shù)據(jù)結構珊蟀。事實上大部分現(xiàn)代計算機語言都以某種形式支持它們菊值。這使得一種數(shù)據(jù)格式在同樣基于這些結構的編程語言之間交換成為可能。
范例:
{
"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."
}
]
- 打印數(shù)組內(nèi)的某個元素
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."
}
- 打印數(shù)組下,全部元素下的某個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"
}
- 打印全部的keys
cat testjson |jq 'keys'
[sysadmin@VM-201-4-centos ~]$ cat 111 |jq 'keys'
[
"connectionCheckUrl",
"core",
"deprecations",
"generationTimestamp",
"id",
"plugins",
"signature",
"updateCenterVersion",
"warnings"
]
[sysadmin@VM-201-4-centos ~]$ cat 111 |jq '.core'
{
"buildDate": "May 31, 2023",
"name": "core",
"sha1": "5x/NLAMG1bqoOpuGKo8xP4lrDZw=",
"sha256": "YAtz6r95eFLjmRlUG4T3aG/2Abl8d7ROsAhD65HH3Ww=",
"size": 98362423,
"url": "https://updates.jenkins.io/download/war/2.401.1/jenkins.war",
"version": "2.401.1"
}