当前位置:网站首页>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")边栏推荐
- UVA 12230 – crossing rivers (probability) "suggested collection"
- 使用枚举实现英文转盲文
- Phoenix JDBC
- Implementation of mahout Pearson correlation
- Mahout-Pearson correlation的实现
- gridView自己定义做时间排版「建议收藏」
- Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
- Cocos2d-x 游戏存档[通俗易懂]
- npm uninstall和rm直接删除的区别
- 权限不足
猜你喜欢

AADL inspector fault tree safety analysis module

万字总结数据存储,三大知识点

Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围

神兵利器——敏感文件发现工具

Static analysis of software defects codesonar 5.2 release

C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)

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

Small guide for rapid formation of manipulator (12): inverse kinematics analysis
![Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
MySQL storage expression error
随机推荐
寫一下跳錶
Introduction to referer and referer policy
恶魔奶爸 A0 英文零基础的自我提升路
数值法求解最优控制问题(〇)——定义
Cocos2d-x game archive [easy to understand]
margin 等高布局
How to meet the dual needs of security and confidentiality of medical devices?
Alibaba cloud award winning experience: how to mount NAS file system through ECS
【C语言】指针进阶---指针你真的学懂了吗?
Implementation of mahout Pearson correlation
使用枚举实现英文转盲文
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
Spark judges that DF is empty
Datatable data conversion to entity
UVA 12230 – crossing rivers (probability) "suggested collection"
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
反诈困境,国有大行如何破局?
How can big state-owned banks break the anti fraud dilemma?
Mysql子查询关键字的使用方式(exists)