当前位置:网站首页>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")边栏推荐
- gridView自己定义做时间排版「建议收藏」
- [UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]
- How to choose financial products? Novice doesn't know anything
- Jetty: configure connector [easy to understand]
- 智能软件分析平台Embold
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- 数值法求解最优控制问题(〇)——定义
- Is private equity legal in China? Is it safe?
- CodeSonar网络研讨会
- Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
猜你喜欢
Lex & yacc of Pisa proxy SQL parsing

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

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization

C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point

Apifox interface integrated management new artifact

Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance

解决使用uni-app MediaError MediaError ErrorCode -5
Klocwork code static analysis tool

How to meet the dual needs of security and confidentiality of medical devices?

使用枚举实现英文转盲文
随机推荐
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
恶魔奶爸 B2 突破语法,完成正统口语练习
Codesonar enhances software reliability through innovative static analysis
单词反转实现「建议收藏」
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
FatMouse&#39; Trade (Hangdian 1009)
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
开户必须往账户里面赚钱吗,资金安全吗?
Phoenix JDBC
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
What are the official stock trading apps in the country? Is it safe to use
object-c编程tips-timer「建议收藏」
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
恶魔奶爸 A1 语音听力初挑战
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
FatMouse&#39; Trade(杭电1009)