Search code examples
excelpowerqueryjson-query

Token Comma expected - can not run JSON query


This is a problem i have working in Excels Power Query. I have this query saved in a variable named "content" which is passed to the call Web.Contents.

I can not run the query, i get "Token Comma expected" error. Can somebody tell what that is about?

`let
content = "{
"query": [
    {
      "code": "Region",
      "selection": {
        "filter": "vs:RegionKommun07",
        "values": [
          "1283"
        ]
      }
    },
    {
      "code": "Sysselsattning",
      "selection": {
        "filter": "item",
        "values": [
          "FÖRV"
        ]
      }
    },
    {
      "code": "Alder",
      "selection": {
        "filter": "item",
        "values": [
          "30-34"
        ]
      }
    },
    {
      "code": "Kon",
      "selection": {
        "filter": "item",
        "values": [
          "1"
        ]
      }
    },
    {
      "code": "Tid",
      "selection": {
        "filter": "item",
        "values": [
          "2015"
        ]
      }
    }
  ],
  "response": {
    "format": "px"
  }
}",
Source = Json.Document(Web.Contents("http://api.scb.se/OV0104/v1/doris/sv/ssd/START/AM/AM0207/AM0207H/BefSyssAldKonK", [Content=Text.ToBinary(content)]))
in
    Source`

Solution

  • If you want " inside a quoted string then you need to double them up like "" to escape them.

    let
    content = "{
    ""query"": [
        {
          ""code"": ""Region"",
          ""selection"": {
            ""filter"": ""vs:RegionKommun07"",
            ""values"": [
              ""1283""
            ]
          }
        },
        ...
        ...
        }"
    

    See page 21 here: http://download.microsoft.com/download/8/1/A/81A62C9B-04D5-4B6D-B162-D28E4D848552/Power%20Query%20M%20Formula%20Language%20Specification%20(July%202019).pdf

    To include quotes in a text value, the quote mark is repeated, as follows: "The ""quoted"" text" // The "quoted" text