'Dynamically referring to JSON node in Power Query M

I have a function that extracts a node from JSON document as follows:

...
Json  = GetJson(Url),
Value = Json[#"values"]

values correspond to the actual node within the JSON document.

I would like to generalize this piece of code and provide the name of the node as a variable like:

let myFunc = (parentNodeName as text) =>
...
Json  = GetJson(Url),
Value = Json[parentNodeName]

However getting this error: An error occurred in the ‘myFunc’ query. Expression.Error: The field 'parentNodeName' of the record wasn't found.

How can I refer to the JSON node dynamically?



Solution 1:[1]

Try

(Json, parentNodeName ) =>
let
...
Value = Record.Field(Json,parentNodeName)
in Value

sample code:

let Json = Json.Document(Web.Contents("http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json")),
Value=myFunc(Json,"title")
in Value

and myFunc:

(Json, parentNodeName ) =>
let 
Value = Record.Field(Json,parentNodeName)
in Value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 horseyride