导言
工作中时常要上传json数据,上传前做校对时,单行的一长串json数据不方便校对,复制到 vscode中用插件实现格式化又麻烦,故有了写个脚本工具实现格式化输出json的想法。
Githubhttps://github.com/pedroqin/shell_script
实现效果将单行json数据格式化为有缩进层次的多行文本。 示例:
1apple@Pedro-Mac-mini ~/D/json_tool> ./format_json.sh -f json.txt 2{ 3 "message" : "success感谢又拍云(upyun.com)提供CDN赞助", 4 "status" : 200, 5 "date" : "20200422", 6 "time" : "2020-04-22 22:22:23", 7 "cityInfo" : { 8 "city" : "天津市", 9 "citykey" : "101030100", 10 "parent" : "天津", 11 "updateTime" : "17:33" 12 }, 13 "data" : { 14 "shidu" : "8%", 15 "pm25" : 11.0, 16 "pm10" : 31.0, 17 "quality" : "优", 18 "wendu" : "15", 19 "ganmao" : "各类人群可自由活动", 20 "forecast" : [ 21 { 22 "date" : "22", 23 "high" : "高温 16℃", 24 "low" : "低温 6℃", 25 "ymd" : "2020-04-22", 26 "week" : "星期三", 27 "sunrise" : "05:26", 28 "sunset" : "18:55", 29 "aqi" : 37, 30 "fx" : "西北风", 31 "fl" : "4-5级", 32 "type" : "晴", 33 "notice" : "愿你拥有比阳光明媚的心情" 34 } 35 ], 36 "yesterday" : { 37 "date" : "21", 38 "high" : "高温 15℃", 39 "low" : "低温 6℃", 40 "ymd" : "2020-04-21", 41 "week" : "星期二", 42 "sunrise" : "05:28", 43 "sunset" : "18:54", 44 "aqi" : 60, 45 "fx" : "西北风", 46 "fl" : "5-6级", 47 "type" : "晴", 48 "notice" : "愿你拥有比阳光明媚的心情" 49 } 50 } 51}实现思路视整个json数据为一个字串,依次获取每个字符。一般使用的方式有两种如下:使用read每次获取单个字符,但需注意,使用read时需先设置$IFS(Internal Field Seprator),否则空格会直接跳过,另还有其他一些问题,需要考虑的特殊情况较多,实现稍繁琐。1ifs_bak="$IFS" 2IFS=$'n' 3echo "$strings"|while ((1)); do 4 read -s -r -n 1 ch 5 judge_char "$ch" 6 [ -z "$ch" ] && break 7done 8IFS="$ifs_bak"使用字符串截取,无使用read时的特殊情况,推荐1offset=0 2while ((1)); do 3 ch="${strings:$offset:1}" 4 judge_char "$ch" 5 [ -z "$ch" ] && break 6 let offset++ 7done将上一步获取的字符做判断,筛选出六个构造字符(“[”、“]”、“{”、“}”、“:”、“,”),并实现对应缩进长度和换行其他注意点在json 对象和数组中的“[”、“]”、“{”、“}”、“:”、“,” 为普通字符,需加判断,本文处使用flag值做判断实现缩进的关键点在于缩进字串长度的控制,以及六个构造字符打印时伴随的换行和缩进字符的打印代码 1#!/bin/bash 2############################################### 3# Filename : format_json.sh 4# Author : PedroQin 5# Date : 2020-04-22 18:33:35 6# Description : 7# Version : 1.0.1 8############################################### 9 10# you can diy indent ,like "t or " " 11indent=" " 12 13############################################## 14indent_num=0 15indent_str="" 16double_quotes=false 17#A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string. --- by https://www.json.org/json-en.html ,so single quote is not accepted ! 18#single_quote=false 19# show message in green 20function green_message() 21{ 22 echo -ne "