user9302275
user9302275

Reputation:

How to Remove \n and \r from JSON string using Bash?

I've been trying to remove the new line and carriage return indicators from my JSON output using this answer.

I've also tried running this based on another answer I've seen on Stack Overflow, but it still does not appear to be working:

 sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' text.json > text_clean.json

My current .sh script looks like this:

getPage() {
curl --fail -X GET 'https://api.test.com/v1/marx?page=1&pageSize=1000&sort=desc' \
  -H 'Authorization: Bearer xxxxxxx' \
  -H 'cache-control: no-cache'  
}

getPage \
| jq -c '.data[]' \
  > text.json

What am I missing here?

If it helps, an example of the string containing it takes on many different forms in the output, but here's a good one:

Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n

Upvotes: 5

Views: 9824

Answers (3)

LincolnP
LincolnP

Reputation: 204

I would use tr for this.

In you example, try the following:

 echo "Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n"| tr -d '\012\015'

Upvotes: 2

Jebby
Jebby

Reputation: 1955

You can remove the characters by using perl if you have it installed:

echo "Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n" | perl -pe 's/\r\n//g'

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295315

If you want to modify JSON, do it in jq.

In this case, that might mean changing:

getPage \
| jq -c '.data[]' \
> text.json

...to...

getPage \
| jq -c '.data[] | sub("\r\n"; " ") | sub("\n"; " ")' \
> text.json

Upvotes: 3

Related Questions