0

I'm using Bash to extract json keys by 'jsawk'. I converted a some.xlsx to some.json by node xlsx . I have some keys like this:

"Scheduled Release"
"Standard Input Parameters\n(Dev)"

The second one contains '\n' because the cell in the .xlsx has 2 lines.

The problem is that I cannot extract the value, using this:

$ cat some.json | jsawk -n 'out(this.["Scheduled Release"])'
or
$ cat some.json | jsawk -n 'out(this.["Standard Input Parameters\n(Dev)"])'

No problem for the keys without white spaces. My some.json contains an array of objects like this:

{
 "Service": "A service",
 "Resource": "Some Users",
 "Engineer": "A name",
 "Description": "A description",
 "Scheduled Release" : "1.0",
 "Method": "A method",
 "Endpoint": "An enpoint",
 "Standard Input Parameters\n(Dev)": "Some inputs"
}

How can I do this with Bash?

Jeff
  • 1
  • 2
  • That's not a bash issue, not really. That's a `jsawk` issue. That said you can *probably* use `jsawk -n $'out(this.["Standard Input Parameters\n(Dev)"])'` but beware that `$''` quoting expands a number of backslash escapes and so you'll need to be mindful of them with that. (You can scope that to *just* the `\n` if you want too: `jsawk -n 'out(this.["Standard Input Parameters'$'\n''(Dev)"])'`. Though I would almost have expected that to Just Work assuming the key actually contains a literal newline and jsawk is interpreting the literal string there normally. – Etan Reisner Oct 13 '15 at 02:55
  • Thank you @EtanReisner but it did not work. `$' '` may work for stdout, not `jsawk`. – Jeff Oct 13 '15 at 03:45
  • `$''` quoting is a shell feature. It replaces escapes in the string. `jsawk` will see a *literal* newline in the string at that point rather than the two-character sequence `\n`. Whether that works or not depends on how `jsawk` and the input are set up. Can you show a, trimmed, representative input json file that could be tested with? – Etan Reisner Oct 13 '15 at 03:52
  • Please find my updated example @EtanReisner , I want to extract all the values of the key "Scheduled Release", "Standard Input Parameters\n(Dev)",..and so on. – Jeff Oct 13 '15 at 04:12
  • Your snippet file and your exact command work for me with `jsawk` from git and `awk` 3.1.5. (Once I drop the `.` in `this.[` and use `this[` instead that is.) – Etan Reisner Oct 13 '15 at 04:48
  • That works! Thanks @EtanReisner, I tried with `jshon` and it also works with `$' '`. – Jeff Oct 13 '15 at 07:06
  • So was the `.` the problem? That caused `jsawk` to throw an error for me and fail entirely. – Etan Reisner Oct 13 '15 at 11:40
  • Yes, `.` was the problem. Thanks @EtanReisner – Jeff Oct 14 '15 at 03:24
  • Were you not getting `jsawk` parse errors when including the `.` in the string? If you *were* then you should have included that information (and the errors) in your post. Because the post just reads like they didn't output the information you were expecting. – Etan Reisner Oct 14 '15 at 13:12

1 Answers1

0

Now I can extract my expected key using this:

$cat some.json | jsawk -n 'out(this["Standard Input Parameters\n(Dev)"])'

Jeff
  • 1
  • 2