Search code examples
cjsonvisual-studio-codeprintingcode-snippets

How to treat %d as a string in JSON


I am using Visual Code Studio as my IDE, and wanted to write a simple snippet for printing:

printf("%d", );

the snippet:

 "Print": {
    "prefix": "printf",
    "body": [
        "printf("%d",$1);",
        "$2"
    ],
    "description": "print"
}

It is a JSON file, however it gives me an error with %d and doesnt treat %d as a string, I suppose. The error being: Expected commajson(514)


Solution

  • You need to escape double-quotes that appear in strings with a backslash, otherwise it looks like you're ending and restarting the string:

     "Print": {
        "prefix": "printf",
        "body": [
            "printf(\"%d\",$1);",
            "$2"
        ],
        "description": "print"
    }