nick_rinaldi
nick_rinaldi

Reputation: 707

Proper JSON formatting

I am configuring a json file that contains airflow connections, which are loaded in the backend when I launch my airflow server.

One of the fields, requires me to stringify a dictionary pretty much. I'm having some trouble with the syntax however. I understand that in JSON, \" replaces double quotes, but I must be missed something here. Here's an example:

"{\"extra__google_cloud_platform__key_json\":
            \"{
                \"type\": \"service_account\",
                \"project_id\": \"sample-project-id123\",
                \"private_key_id\": \"sample_key_id\"
               }"
 }"

In VS Code, this field is underlined in red, saying things like "Unexpected End of String" and "Colon Expected".

What am I missing here?

Upvotes: -1

Views: 119

Answers (2)

Serge
Serge

Reputation: 43850

If you want just json backlash chars should be removed

var json= {
  "extra__google_cloud_platform__key_json": {
    "type": "service_account",
    "project_id": "sample-project-id123",
    "private_key_id": "sample_key_id"
  }
}

if you want to convert the json to string, it should be in one line

var jsonString="{\"extra__google_cloud_platform__key_json\":{\"type\":\"service_account\",\"project_id\":\"sample-project-id123\",\"private_key_id\":\"sample_key_id\"}}";

or you can create a json string

var jsonString= JSON.stringify(json);

in your string you have a bug - an extra \ " before {

           \"{
                \"type\": \"service_account\",

if you remove it and connect all lines with + , then it will be working too.

var jsonString=
"{"+
" \"extra__google_cloud_platform__key_json\":" +
      " {" +
          " \"type\":\"service_account\"," +
          " \"project_id\":\"sample-project-id123\"," +
          " \"private_key_id\":\"sample_key_id\""+
      " }" +
 "}";

or this is working in javascript too

var jsonString=
'{'+
' \"extra__google_cloud_platform__key_json\":  '+
      ' {' +
          ' \"type\":\"service_account\", '+
          ' \"project_id\":\"sample-project-id123\", '+
          ' \"private_key_id\":\"sample_key_id\"  '+
      ' }' +
 '}';

Upvotes: 1

nick_rinaldi
nick_rinaldi

Reputation: 707

The solution for me, was to include everything in one line.

Upvotes: 0

Related Questions