1

I have a json that looks like this:

{
  "d": {
    "results": [
      {
        "__metadata": {
          "id": "12",
          "uri": "xyz",
          "type": "Result"
        },
        "YEAR": "2018",
        "MOW": "0102",
        "UNIT": "1.2761625",
        "VALUE1": "0",
        "VALUE2": "0",
        "SIGNAL": "0"
      },
      {
        "__metadata": {
          "id": "13",
          "uri": "xyz",
          "type": "Result"
        },
        "YEAR": "2018",
        "MOW": "0102",
        "UNIT": "1.2761625",
        "VALUE1": "0",
        "VALUE2": "0",
        "SIGNAL": "0"
      },

and so on

I want to output every record in one single line like this (because AWS Athena requires this format):

{ "__metadata": {"id": "12", "uri": "xyz", "type": "Result"}, "YEAR": "2018", "MOW": "0102",  "UNIT": "1.2761625", "VALUE1": "0", "VALUE2": "0", "SIGNAL": "0"}
{ "__metadata": {"id": "13", "uri": "xyz", "type": "Result"}, "YEAR": "2018", "MOW": "0102",  "UNIT": "1.2761625", "VALUE1": "0", "VALUE2": "0", "SIGNAL": "0"}

I used " jq -c . myjson.json" for formatting. I expected it to put each record in one single line, but instead it put every record next to each other so my json holds one very long single line.

peak
  • 105,803
  • 17
  • 152
  • 177
incnnu
  • 173
  • 3
  • 14
  • @oguzismail it's not working for me, or maybe I'm using that command wrong? how can I incorporate the '{(.id): .custom}' in my command? – incnnu Apr 26 '20 at 09:49
  • You need only the -c flag. `jq -c '.results[]' file`. – oguz ismail Apr 26 '20 at 10:00
  • What's `'.results[]'` for? I used the -c flag like this `jq -c . myjson.json` and it's only giving me one single line with every record next to each other. – incnnu Apr 26 '20 at 10:16
  • It's for expanding the results array to a stream so that each of its elements will be printed in a separate line – oguz ismail Apr 26 '20 at 10:21
  • @oguzismail I see. I tried the command and I get the error message `error (at myjson.json:0): Cannot iterate over null (null)` – incnnu Apr 26 '20 at 10:26
  • 1
    Yeah there is d before results, I didn't see that. It'll be `jq -c '.d.results[]' file` then. See https://jqplay.org/s/xg5Ni54PIn – oguz ismail Apr 26 '20 at 10:30

0 Answers0