0

I am getting json from and API via curl like so:

$ curl -H 'Accept: application/json'  http://127.0.0.1:8000/snippets/2/
{"id":2,"title":"","code":"print \"hello, work\"\n","linenos":false,"language":"python","style":"friendly"}

… and all is well. But I want to format the json output as a string that will be acceptable to use as form data for a POST request. So I added | sed -e's/\"/\'/g' to the above curl command but the shell is expecting me to provide another quote. See …

$ curl -H 'Accept: application/json'  http://127.0.0.1:8000/snippets/2/  | sed -e's/\"/\'/g'
>
>
>
> '
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0sed: -e expression #1, char 8: unterminated `s' command
100   107  100   107    0     0  13375      0 --:--:-- --:--:-- --:--:-- 13375
(23) Failed writing body

I was hoping to turn all the "'s in the output to ''s.

Update: Thank for pointing out answer How to escape single quotes within single quoted strings? but I do not think changing my command line to ...

$ curl -H 'Accept: application/json'  http://127.0.0.1:8000/snippets/2/  | sed -e's/'"'"'/"'"'"/g'

... will solve my problem ... I'll try it. Tried it and got same error.

Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • 1
    Not sure what `'s/'"'"'/"'"'"/g'` is supposed to be. `'s/"/'\''/'` should be enough. – muru Oct 31 '18 at 04:26
  • thanks @muru I did this `$ curl -H 'Accept: application/json' http://127.0.0.1:8000/snippets/2/ | sed -e's/"/'\''/g'` and got this output, `{'id':2,'title':'','code':'print \'hello, work\'\n','linenos':false,'language':'python','style':'friendly'}`. – Red Cricket Oct 31 '18 at 04:40
  • 4
    Why do you think single quotes will make this more acceptable for a POST request? – tripleee Oct 31 '18 at 04:48
  • Never use `sed` to work with JSON in the first place. Use a tool like `jq` that knows how to correctly parse and construct JSON. – chepner Oct 31 '18 at 12:51
  • @triplee You're right the bit about wanting to generate formdata for a POST I could have left out of the question, as I was really just curious about what I was doing wrong with my `sed` command. – Red Cricket Oct 31 '18 at 16:26

0 Answers0