当前位置:网站首页>Awk processing JSON processing
Awk processing JSON processing
2022-07-07 21:15:00 【Chestnut less】
Nested parsing and lists will be a little bug... However, it meets the requirements of light use
#!/bin/bash
json_str='{
"access_token":"12345678-1234-1234-1234-123456789012",
"token_type":"bearer",
"refresh_token":"12345678-1234-1234-1234-12345678901"
}'
get_json_value() {
awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
foundKeyCount = 0
while (length(json) > 0) {
pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}
++foundKeyCount;
start = 0; stop = 0; layer = 0;
for (i = pos + length(key) + 1; i <= length(json); ++i) {
lastChar = substr(json, i - 1, 1)
currChar = substr(json, i, 1)
if (start <= 0) {
if (lastChar == ":") {
start = currChar == " " ? i + 1: i;
if (currChar == "{" || currChar == "[") {
layer = 1;
}
}
} else {
if (currChar == "{" || currChar == "[") {
++layer;
}
if (currChar == "}" || currChar == "]") {
--layer;
}
if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
stop = currChar == "," ? i : i + 1 + layer;
break;
}
}
}
if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
if (foundKeyCount == 0) {print defaultValue;} exit 0;
} else {
print substr(json, start, stop - start);
}
json = substr(json, stop + 1, length(json) - stop)
}
}'
}
echo $(get_json_value "${json_str}" "refresh_token")边栏推荐
- npm uninstall和rm直接删除的区别
- 如何满足医疗设备对安全性和保密性的双重需求?
- You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
- Implement secondary index with Gaussian redis
- 95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
- OneSpin | 解决IC设计中的硬件木马和安全信任问题
- How to meet the dual needs of security and confidentiality of medical devices?
- 智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
- [paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
- CodeSonar网络研讨会
猜你喜欢
CodeSonar通过创新型静态分析增强软件可靠性
Klocwork 代码静态分析工具
MySQL约束之默认约束default与零填充约束zerofill

H3C s7000/s7500e/10500 series post stack BFD detection configuration method

AADL inspector fault tree safety analysis module

程序猿赚的那点钱算个P啊!

Codesonar Webinar

OneSpin | 解决IC设计中的硬件木马和安全信任问题

解决使用uni-app MediaError MediaError ErrorCode -5

C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
随机推荐
AADL inspector fault tree safety analysis module
Klocwork 代码静态分析工具
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
FTP steps for downloading files from Huawei CE switches
UVA 12230 – crossing rivers (probability) "suggested collection"
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
EasyUI date control emptying value
Solve the problem of using uni app mediaerror mediaerror errorcode -5
寫一下跳錶
Do you have to make money in the account to open an account? Is the fund safe?
恶魔奶爸 A0 英文零基础的自我提升路
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
How to meet the dual needs of security and confidentiality of medical devices?
Implementation of mahout Pearson correlation
uva 12230 – Crossing Rivers(概率)「建议收藏」
万字总结数据存储,三大知识点
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
Codeforces 474 F. Ant colony
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]