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.

2
  • Possible duplicate of send/post xml file using curl command line Commented Nov 17, 2016 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. Commented Nov 17, 2016 at 15:25

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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!
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 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.
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.
0

Use jq's JSON serialization capability:

cat my_file.js | jq '@json'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.