0

I'm trying to write a small script to upload snippets of code to a service provider we use that requires you to pass that code inside a JSON object.

file_content=$(<my_file.js)
curl -X POST  -H "Content-Type: application/json"
     -d '{"name":$file_name","script":"$file_content"}' https://someservice.com/api/endpoint

Where file_content is the javascript code inside my_file. The problem is that printed just like that, the payload is invalid. I'm trying to find a way to read that file in a way that it's valid json. I know it's rather specific but wondering if such command exists.

EDIT: Another option would be to just place the entire JSON object in a file, but i would like to avoid that if possible.

acadavid
  • 506
  • 6
  • 14
  • Possible duplicate of [send/post xml file using curl command line](http://stackoverflow.com/questions/3007253/send-post-xml-file-using-curl-command-line) – Tomalak Nov 17 '16 at 15:03
  • @Tomalak It's similar but it would require that i move the entire JSON object to the file itself. If i can't find a solution, i'll go for this one. – acadavid Nov 17 '16 at 15:25

2 Answers2

1

Except in the very simplest of cases, you can't build JSON like that, i.e. by string interpolation. Putting a file's content into a JSON string is not a simple case.

JSON needs to be built by an actual JSON serializer. Python has such a serializer, it would be easy to let Python do the job.

# from http://stackoverflow.com/a/13466143
json_escape () {
    printf '%s' $1 | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}

so you can do proper value encoding:

file_content=$(<my_file.js)
json_file_name=$(json_escape "fancy filename")
json_file_content=$(json_escape "$file_content")
json='{"name":'$json_file_name',"script":'$json_file_content'}'

curl -X POST -H "Content-Type: application/json" -d "$json" https://someservice.com/api/endpoint
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Yes, i think what i was trying to do was a bit unreasonable to do using more primitive tools. Thanks a lot for your help! – acadavid Nov 17 '16 at 16:44
  • There is no proper JSON handling in bash, and everything I've found so far is either blatantly wrong or uses external tools. There are tools for JSON manipulation (like https://stedolan.github.io/jq/) but they are likely not in a standard distribution of Linux, making portability is an issue. Python however is most likely available, it's mature and it's fast. If all your script does is send data to an API, I'd recommend turning it into a Python script entirely. You can use Python to make the HTTP request and possibly handle the response. – Tomalak Nov 17 '16 at 16:51
  • 1
    Agreed. At the end i went for a Ruby script and that was way easier and simple, i was just playing around with this idea but at the end it's just simpler to do it with proper tools. – acadavid Nov 18 '16 at 07:07
0

Use jq's JSON serialization capability:

cat my_file.js | jq '@json'
Hans Z.
  • 50,496
  • 12
  • 102
  • 115