2

The wikipedia API returns JSON that contains an element with the name *. How can I access such a property in PowerShell?

Example:

$json = Invoke-RestMethod -Method Get -Uri 'https://en.wikipedia.org/w/api.php?action=parse&pageid=10000&prop=wikitext&format=json'
$json.parse.wikitext.* # fails

Error: "Missing property name after reference operator." So the asterisk is not recognized as a property name.

JSON excerpt:

{ 
   "parse": { 
      "title": "Eiffel",
      "pageid": 10000,
      "wikitext": { 
         "*": "{{Wiktionary|Eiffel}}\n'''Eiffel''' ..."
      }
   }
}
stackprotector
  • 10,498
  • 4
  • 35
  • 64

1 Answers1

5

Put it in quotation marks:

$json.parse.wikitext."*"
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • 5
    If I may give my two cents in the matter, when dealing with text that may be interpreted as a special character, use single quotation marks, that way PowerShell will read the string literally. – Orel Fichman Jul 08 '20 at 16:00
  • @OrelFichman That doesn't make any difference here. What you're referring to are escape characters and string interpolation. Single quotes represent string literals. – Maximilian Burszley Jul 08 '20 at 16:07
  • 4
    @MaximilianBurszley True, but Orel's advice makes sense _as a good general habit to form_: if you know that something is a _literal_ (verbatim) string, use `'...'`; if you need _string expansion_, i.e. interpolation of escape sequences, variable references and expressions embedded in a string, use `"..."`. From that perspective, given that the `*` is to be used _verbatim_, `$json.parse.wikitext.'*'` as the solution is preferable here. – mklement0 Jul 08 '20 at 16:37