0

We have a JSON file that needs to be multi-lined instead of being single lined as shown below,

{this is the first block},{this is the second block, really},{this is the third you're kidding:no}

We expected it to be like this so that it can be fed to an external program to read it without issues,

{this is the first block}
{this is the second block, really}
{this is the third you're kidding:no}

I'm not an expert with simple text processing tools like awk, sed, cut but I did try with sed for sometime unsuccessfully.

cat test.json | sed 's/},/\n/g'
{this is the first block
{this is the second block, really
{this is the third you're kidding:no}

What is the best way to do this?

5
  • 1
    seems like you need }\n instead of \n in your sed attempt Commented Nov 18, 2020 at 3:29
  • Your inputs are not JSON Commented Nov 18, 2020 at 3:29
  • 1
    To be JSON, it would need to be in an array, starting with [ and ending with ]. Is that the case for your real data? Commented Nov 18, 2020 at 3:35
  • 2
    (if so, jq -c '.[]' <infile.json will do the job). Commented Nov 18, 2020 at 3:36
  • 2
    To illustrate @CharlesDuffy's comment echo '[{"a":0},{"b":1},{"c":2}]' | jq -c '.[]' Commented Nov 18, 2020 at 3:41

2 Answers 2

3

Since it is not JSON and you just want to split:

input_file

{this is the first block},{this is the second block, really},{this is the third you're kidding:no}
sed 's/},/}\n/g' input_file

output

{this is the first block}
{this is the second block, really}
{this is the third you're kidding:no}
Sign up to request clarification or add additional context in comments.

Comments

1

Awk alternative:

awk 'BEGIN { RS="(,?{)|(},?)" } /[[:alnum:]]/ { print $0="{"$0"}" }' file

Set the records separator to one or more commas and { or } and one or more commas. Then, when an alpha numeric string is encountered, prefix { to the string, append } and print

Comments

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.