Remove escape characters like newline, tab and carriage return from JSON file

I have JSON with 80+ fields. By selecting the message field in the JSON file below using jq, I get newlines and tabs. I want to remove escape characters and I tried it with sed, but that didn't work.

Example JSON file:

{
"HOSTNAME":"server1.example",
"level":"WARN",
"level_value":30000,
"logger_name":"server1.example.adapter",
"content":{"message":"ERROR LALALLA\nERROR INFO NANANAN\tSOME MORE ERROR INFO\nBABABABABABBA\n BABABABA\t ABABBABAA\n\n BABABABAB\n\n"}
}

Can someone help me with this?

+4
source share
2 answers

A clean solutionjq :

$ jq -r '.content.message | gsub("[\\n\\t]"; "")' file.json
ERROR LALALLAERROR INFO NANANANSOME MORE ERROR INFOBABABABABABBA BABABABA ABABBABAA BABABABAB

If you want to keep characters ", omit -r.

: , Unicode ASCII Latin-1 , \p{Cc}. jq Oniguruma regex.


, sed tr.

sed \n t:

$ jq '.content.message' file.json | sed 's/\\[tn]//g'
"ERROR LALALLAERROR INFO NANANANSOME MORE ERROR INFOBABABABABABBA BABABABA ABABBABAA BABABABAB"

, " . , sed:

$ jq '.content.message' file.json | sed 's/\\[tn]//g; s/"\(.*\)"/\1/'
ERROR LALALLAERROR INFO NANANANSOME MORE ERROR INFOBABABABABABBA BABABABA ABABBABAA BABABABAB

, " (: \n):

$ jq -r '.content.message' file.json | tr -d '\n\t'
ERROR LALALLAERROR INFO NANANANSOME MORE ERROR INFOBABABABABABBA BABABABA ABABBABAA BABABABAB

, -r , jq ( \n \t), - - tr.

+2

:

$ jq 'walk(if type == "string" then gsub("\\p{Cc}"; "<>") else . end)' 

:

{
  "HOSTNAME": "server1.example",
  "content": {
    "message": "ERROR LALALLA<>ERROR INFO NANANAN<>SOME MORE ERROR INFO<>BABABABABABBA<> BABABABA<> ABABBABAA<><> BABABABAB<><>"
  },
  "level": "WARN",
  "level_value": 30000,
  "logger_name": "server1.example.adapter"
}

, :

  • walk/1. (walk/1 JSON.)
  • gsub/2 invocations.
  • , "" gsub/2.

walk/1, jq , ( , ) .

+3

Source: https://habr.com/ru/post/1659273/


All Articles