-3

For example, the file looks like this:

{
  "a":1
}
{
  "b":2
}

I want turn it into :

{"a":1}
{"b":2}

How can I do that in bash ?

Lewis Chan
  • 695
  • 6
  • 18
  • That's not valid JSON. – oguz ismail Jul 17 '21 at 04:20
  • yes, But I've explained my intention. – Lewis Chan Jul 17 '21 at 04:24
  • If you provide valid sample JSON data it's a lot easier to test answers. – Shawn Jul 17 '21 at 04:37
  • Please provide actual, valid sample data (*yes, but I've explained my intention* does not work) and your attempts to solve the problem yourself. SO is not a free code writing service. We're more than happy to help, but we expect you to make an effort to solve the problem first. Once you've done that and run into difficulties, you can explain the problem you're having, include the relevant portions of your code in the form of a [mre], and ask a specific question related to that code, and we'll try to help. – Ken White Jul 17 '21 at 04:38
  • Sorry for that. I forgot to add quotes – Lewis Chan Jul 17 '21 at 04:43
  • Duplicate of https://stackoverflow.com/questions/61125013/how-can-i-minify-json-in-a-shell-script/61125014. – Reino Jul 17 '21 at 09:34

1 Answers1

1

Trivial with jq:

$ jq -c . input.json
{"a":1}
{"b":2}

The -c option stands for compact output, with each value on a single line.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • Wow, I used jq, but haven't known it can deal with multiple `{ }` pairs. – Lewis Chan Jul 17 '21 at 04:54
  • @LewisChan Yup, your file has two JSON objects in it. `jq` will process one at a time until until the entire file's been read (Unlike a lot of tools that expect a file to only contain one JSON value) – Shawn Jul 17 '21 at 04:55