# Quoting JSON in shell scripts for API requests

Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2FAPI%2Fquoting-json.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2FAPI%2Fquoting-json.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

When using `curl` or similar command-line tools to make HTTP API requests, it’s important to understand how your shell processes quotes in command arguments. This becomes especially relevant when the payload includes JSON — and even more so when the JSON contains both single and double quotes.

This guide explains:

* Why quoting breaks when used naively
* How to structure payloads safely using heredoc
* Other viable alternatives

Note

The complexity described in this guide is not caused by DataPrime or its APIs. It stems entirely from how shell environments (like bash or zsh) interpret quoted strings. When integrating with DataPrime programmatically, no escaping is required — you can pass JSON strings directly and cleanly.

## Common pitfall: single quotes in JSON[​](#common-pitfall-single-quotes-in-json "Direct link to Common pitfall: single quotes in JSON")

Here's a basic `curl` example that works fine with simple JSON:

```
curl --location 'https://api.eu2.coralogix.com/api/v1/dataprime/query' \

--header 'Content-Type: application/json' \

--header 'Authorization: Bearer <cx_api_key>' \

--data '{

    "query": "source logs | limit 10"

}'
```

However, if your query includes single quotes, like:

```
source logs | filter log_obj.applicationName == 'my-app'
```

Trying to include that directly in the `--data` flag without escaping will cause the shell to break the string early, resulting in a malformed request. This happens because the shell **does not allow single quotes inside single-quoted strings**. When the shell sees a `'` character, it assumes the string has ended, which corrupts the structure of your JSON. To work around this, you need to escape single quotes properly.

## Best practice: use heredoc to avoid escaping[​](#best-practice-use-heredoc-to-avoid-escaping "Direct link to Best practice: use heredoc to avoid escaping")

To avoid this entirely, use a [heredoc](https://linuxize.com/post/bash-heredoc/) — a standard shell feature (unrelated to DataPrime) that lets you safely pass multi-line strings into a command.

### What is a heredoc?[​](#what-is-a-heredoc "Direct link to What is a heredoc?")

A heredoc is a generic syntax in Unix-like shells that allows passing blocks of text without escape characters. It looks like this:

```
cat <<END

Hello 'world' and "everyone"

EN
```

### Heredoc example for JSON payload[​](#heredoc-example-for-json-payload "Direct link to Heredoc example for JSON payload")

```
curl --location 'https://api.eu2.coralogix.com/api/v1/dataprime/query' \

--header 'Content-Type: application/json' \

--header 'Authorization: Bearer <cx_api_key>' \

--data @- <<EOF

{

  "query": "source logs | filter log_obj.applicationName == 'my-app'"

}

EOF
```

No escaping is needed. It's clean, readable, and robust. You can replace `EOF` with any token, as long as it matches at both ends.

## Alternative: escaping single quotes in bash[​](#alternative-escaping-single-quotes-in-bash "Direct link to Alternative: escaping single quotes in bash")

You can manually insert single quotes inside a single-quoted string using this technique:

```
curl -X POST https://your-api-endpoint.com/query \

  -H "Authorization: Bearer $TOKEN" \

  -H "Content-Type: application/json" \

  -d '{"query": "source logs | filter $l.applicationname == '\''my-app'\''"}'
```

The outer string is wrapped in single quotes. The `'\''` sequence:

* Ends the current string with `'`.

* Inserts a literal single quote with `\'`.

  As the shell is interpreting this, there’s a need to escape the single quote with a backslash. Otherwise, the shell will assume that it’s just the beginning of another single-quoted string.

* Starts a new string with `'`.

This alternative works but may be hard to read and maintain.

## Alternative: double quotes with escaped inner quotes[​](#alternative-double-quotes-with-escaped-inner-quotes "Direct link to Alternative: double quotes with escaped inner quotes")

You can also wrap the JSON payload in double quotes, but then every `"` inside the JSON must be escaped. Note, in the example below, that there are double quotes right after the `--data` param:

```
curl --location 'https://api.eu2.coralogix.com/api/v1/dataprime/query' \

--header "Content-Type: application/json" \

--header "Authorization: Bearer <cx_api_key>" \

--data "{\"query\": \"source logs | filter log_obj.applicationName == 'my-app'\"}"
```

This is technically valid but gets cluttered fast.

## Final takeaway[​](#final-takeaway "Direct link to Final takeaway")

Quoting challenges arise from your shell — not from DataPrime.

When working with DataPrime’s programmatic APIs, you can use native JSON formatting directly without escaping or shell workarounds.

## Additional resources[​](#additional-resources "Direct link to Additional resources")

|                     |                                                                                                 |
| ------------------- | ----------------------------------------------------------------------------------------------- |
| Coralogix Endpoints | [Coralogix Endpoints](https://docs-docusaurus.kinsta.page/integrations/coralogix-endpoints/.md) |
