1

Might be easy enough for someone who has experience with jq. I am novice. I am learning jq with bash to create json arrays. I want to generate json array with jq in bash.

I have my file named fileForV:

V    Apple is good for you
A    Broccoli is good for you

Note: The space is a tab.

I have:

jq -R '[inputs | 
split("\t") | 
{"FruitOrVeg":.[0],"Good?":.[1]}]'<<EOF fileForV EOF

I get:

[
  {
   "FruitOrVeg": "A",
   "Good?": "Apple is good for you"
  },
  {
   "FruitOrVeg": "V",
   "Good?": "Broccoli is good for you\""
  }
]

Why do I get \"" at the end?

Should be:

[
  {
   "FruitOrVeg": "A",
   "Good?": "Apple is good for you"
  },
  {
   "FruitOrVeg": "V",
   "Good?": "Broccoli is good for you"
  }
]
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Woods
  • 81
  • 1
  • 2
  • 8

1 Answers1

1

There's something fishy here. If you are using inputs and getting anything like the results as shown, then:

a) you would have to be using the -n command-line option;

b) you would almost certainly be using jq version 1.5 or later, but I've verified that these versions (in conjunction with bash) definitely do not produce the extraneous double-quotation mark.

Here is a typical transcript, using the input as shown but with one tab per line:

$ jq-1.5 -n -R '
[inputs | split("\t") | {"FruitOrVeg":.[0],"Good?":.[1]}]' input.tsv
[
  {
    "FruitOrVeg": "V",
    "Good?": "Apple is good for you"
  },
  {
    "FruitOrVeg": "A",
    "Good?": "Broccoli is good for you"
  }
]
peak
  • 105,803
  • 17
  • 152
  • 177