如何使用bash脚本将json数据转换为单个字符串,中间没有空格?
我有下面的JSON数据
[
{
"name": "Invalid_Auth",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "Invalid_Auth_Empty",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer ",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "Invalid_Auth_SQL",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@Injection}}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "Default",
"authType": "Basic",
"username": "user1@netbanking.io",
"password": "Welcome@4321",
"header_1": null,
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "UserA",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "UserB",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "UserC",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "ROLE_USER",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "ROLE_PM",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
},
{
"name": "ROLE_ADMIN",
"authType": "Token",
"username": null,
"password": "PASSWORD-MASKED",
"header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{\"username\":\"admin\",\"password\":\"secret\"}' -H \"Content-Type: application/json\" -H \"Accept: application/json\" -X POST https://ip/user/login | jq --raw-output \".info.token\" }}",
"header_2": null,
"header_3": null,
"clientId": null,
"clientSecret": null,
"id": null,
"accessTokenUri": null,
"authorizationScheme": null,
"clientAuthenticationScheme": null,
"tokenName": null,
"scope": null,
"grantType": null,
"preEstablishedRedirectUri": null,
"useCurrentUri": null,
"userAuthorizationUri": null,
"inactive": false,
"invalid": true,
"lastTestedOn": null,
"passwordMasked": true
}
]
我们需要把上面的JSON数据转换成下面的单行JSON数据,没有像下面的空格。
[{"name":"Invalid_Auth","authType":"Token","username":null,"password":"PASSWORD-MASKED","header_2":null,"header_3":null,"clientId":null,"clientSecret":null,"id":null,"accessTokenUri":null,"authorizationScheme":null,"clientAuthenticationScheme":null,"tokenName":null,"scope":null,"grantType":null,"preEstablishedRedirectUri":null,"useCurrentUri":null,"userAuthorizationUri":null,"inactive":false,"invalid":true,"lastTestedOn":null,"passwordMasked":true},{"name":"Invalid_Auth_Empty","authType":"Token","username":null,"password":"PASSWORD-MASKED","header_2":null,"header_3":null,"clientId":null,"clientSecret":null,"id":null,"accessTokenUri":null,"authorizationScheme":null,"clientAuthenticationScheme":null,"tokenName":null,"scope":null,"grantType":null,"preEstablishedRedirectUri":null,"useCurrentUri":null,"userAuthorizationUri":null,"inactive":false,"invalid":true,"lastTestedOn":null,"passwordMasked":true},{"name":"Invalid_Auth_SQL","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "Default","authType": "Basic","username": "user1@netbanking.io","password": "Welcome@4321","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "UserA","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "UserB","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "UserC","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "ROLE_USER","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "ROLE_PM","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true},{"name": "ROLE_ADMIN","authType": "Token","username": null,"password": "PASSWORD-MASKED","header_2": null,"header_3": null,"clientId": null,"clientSecret": null,"id": null,"accessTokenUri": null,"authorizationScheme": null,"clientAuthenticationScheme": null,"tokenName": null,"scope": null,"grantType": null,"preEstablishedRedirectUri": null,"useCurrentUri": null,"userAuthorizationUri": null,"inactive": false,"invalid": true,"lastTestedOn": null,"passwordMasked": true}]
我尝试使用tr
工具和以下命令转换此文件
# assuming
mAuth=$(echo "$Auth" | jq -R . | tr -d ' ')
但是一些字段的键和值之间有空格。
那么我们如何使用bash脚本将json数据转换为单行json数据,并显示上面的示例输出呢?
1条答案
按热度按时间wd2eg0qa1#
让jq来执行此操作:-c是--compact输出选项。