Can not send influx query via grafana API while creating the dashboard

I am creating the grafana dashboards with panels using Grafana API’s. (Influx as datasource)

This is what I tried. (My code to create dashboard )
import requests
url = “http://admin:admin@community.grafana.com/api/dashboards/db
payload =’’’{
“datasource”: null,
“dashboard”: {
“id”: null,
“editable”: true,
“panels”: [
{
“aliasColors”: {},
“bars”: false,
“dashLength”: 10,
“dashes”: false,
“datasource”: “Influx_DataSource”,
“fill”: 1,
“gridPos”: {
“h”: 9,
“w”: 12,
“x”: 0,
“y”: 0
},
“id”: 2,
“legend”: {
“avg”: false,
“current”: false,
“max”: false,
“min”: false,
“show”: true,
“total”: false,
“values”: false
},
“lines”: true,
“linewidth”: 1,
“links”: [],
“nullPointMode”: “null”,
“percentage”: false,
“pointradius”: 5,
“points”: false,
“renderer”: “flot”,
“seriesOverrides”: [],
“spaceLength”: 10,
“stack”: false,
“steppedLine”: false,
“targets”: [
{
“groupBy”: [
{
“params”: [
“$__interval”
],
“type”: “time”
},
{
“params”: [
“null”
],
“type”: “fill”
}
],
“orderByTime”: “ASC”,
“policy”: “default”,
“query”: " ",
“rawQuery”: true,
“refId”: “A”,
“resultFormat”: “time_series”,
“select”: [
[
{
“params”: [
“value”
],
“type”: “field”
},
{
“params”: [],
“type”: “mean”
}
]
],
“tags”: []
}
],
“thresholds”: [],
“timeFrom”: null,
“timeShift”: null,
“title”: “ResponseTime”,
“tooltip”: {
“shared”: true,
“sort”: 0,
“value_type”: “individual”
},
“type”: “graph”,
“xaxis”: {
“buckets”: null,
“mode”: “time”,
“name”: null,
“show”: true,
“values”: []
},
“yaxes”: [
{
“format”: “ms”,
“label”: null,
“logBase”: 1,
“max”: null,
“min”: null,
“show”: true
},
{
“format”: “short”,
“label”: null,
“logBase”: 1,
“max”: null,
“min”: null,
“show”: true
}
],
“yaxis”: {
“align”: false,
“alignLevel”: null
}
}
],
“title”: “d1”,
“originalTitle”: “New dashboard”,
“timezone”: “browser”,
“schemaVersion”: 6,
“style”: “dark”,
“version”: 0,
“templating”: {
“from”: “now-6h”,
“to”: “now”
},
“timepicker”: {
“refresh_intervals”: [
“5s”,
“10s”,
“30s”,
“1m”,
“5m”,
“15m”,
“30m”,
“1h”,
“2h”,
“1d”
],
“time_options”: [
“5m”,
“15m”,
“1h”,
“6h”,
“12h”,
“24h”,
“2d”,
“7d”,
“30d”
]
},
“timezone”: “browser”,
“title”: “D1”,
“uid”: “dash1”,
“version”: 1,
“overwrite”: false
},
“folderId”: 80,
“overwrite”: false
}’’’
headers={“Content-Type”: ‘application/json’}
response = requests.post(url, data=payload,headers=headers)
print (response.text)


In Above code


“targets”: [
“query”: " ",
],
Result: Creates the dashboard.

“targets”: [
“query”: "SELECT “responseTime” FROM “requestsRaw” WHERE $timeFilter ",
],
Result:
I am getting error as -
[{“classification”:“DeserializationError”,“message”:“invalid character ‘r’ after object key:value pair”},{“fieldNames”:[“Dashboard”],“classification”:“RequiredError”,“message”:“Required”}]

Can any one help me to solve this issue

This is not valid json syntax (nested double quotes):

“query”: "SELECT “responseTime” FROM “requestsRaw” WHERE $timeFilter "

Escape them:

“query”: "SELECT \“responseTime\” FROM \“requestsRaw\” WHERE $timeFilter "

Also you may use Grafana UI/editor and export dashboard from Grafana - it will escape all special characters.

I tried this but still have same issue. query

Issue

Thanks This issue got solved by adding query like this

“query”: “SELECT responseTime FROM requestsRaw WHERE requestName =~ /^request/ AND testName =~/^testName/ AND $timeFilter”,

And I am able to create the dashboard .
Now I am facing new issue that is The variables and Annotations are not creating in created dashboard

This is what I tried

  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      },
      {
        "datasource": "Yajana_DataSource",
        "enable": false,
        "hide": false,
        "iconColor": "#0a437c",
        "limit": 100,
        "name": "Test Start",
        "query": "SELECT * FROM testStartEnd WHERE type='started'",
        "showIn": 0,
        "tags": [],
        "type": "tags"
      },
      {
        "datasource": "Yajana_DataSource",
        "enable": false,
        "hide": false,
        "iconColor": "rgba(255, 96, 96, 1)",
        "limit": 100,
        "name": "Test end",
        "query": "SELECT * FROM testStartEnd WHERE type='finished'",
        "showIn": 0,
        "tags": [],
        "type": "tags"
      }
   ]
  },

Thanks.