Home > Mobile >  how to convert json data into single string with no spaces in between using bash scripting?
how to convert json data into single string with no spaces in between using bash scripting?

Time:02-01

how to convert json data into single string with no spaces in between using bash scripting?

I have below json data

[
  {
    "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": "[email protected]",
    "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
  }
]

we need to convert above json data into below single line json data with no spaces like below one.

[{"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": "[email protected]","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}]

I tried to convert this using tr tool with below command

# assuming 
mAuth=$(echo "$Auth" | jq -R . | tr -d ' ')

But some fields had spaces between key and values.

So how we can convert that json data into single line json data with the sample output shown above using bash scripting?

CodePudding user response:

Let do it: -c is the --compact-output option.

mAuth=$(echo "$Auth" | jq -c .)
  • Related