0

I have the following string:

"{\n  \"type\": \"service_account\",\n  \"project_id\": \"my-project-123\",\n  \"time\": \"23:00:22.23124\"\n  }"

And I want a formatted JSON like this:

{
    "type":"service_account",
    "project_id":"my-project-123",
    "time":"23:00:22.23124"
}
4
  • What produced that string? Commented Apr 3, 2020 at 19:22
  • Are the outer quotes part of the string, i.e., do you have x='"{\n \"type..."' or x="{\n \"type..."? Commented Apr 3, 2020 at 19:35
  • The string is inside a txt file. Commented Apr 3, 2020 at 20:08
  • Did something encode a JSON value as a JSON string? You might want to use something like jq 'fromjson' yourfile.json Commented Apr 3, 2020 at 20:13

1 Answer 1

1

The -e option can expand \n within a string.

echo -e "put your string here"
Sign up to request clarification or add additional context in comments.

7 Comments

printf "%s" ?
I suggest to replace %s with %b.
Anything that replaces echo -e ;)
This will expand all escape sequences, even if they occur inside JSON strings and should be treated literally.
@chepner: This is correct and possibly not intended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.