Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
gsara45
Regular Visitor

Fetch value from JSON response

Hi,

I would like to fetch "red" value based on "objectTypeAttributeId": "1235". Can anyone help me with the expression to fetch ?

 

JSON response:

 

"attributes": [
{
"workspaceId": "xxx-xxxx-xxxx-xxxx-xxxxxxxx",
"globalId": "xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx",
"id": "12345678",
"objectTypeAttributeId": "1234",
"objectAttributeValues": [
{
"value": "ABCD-1264",
"searchValue": "ABCD-1264",
"referencedType": false,
"displayValue": "ABCD-1264"
}
],
"objectId": "1264533"
},
{
"workspaceId": "xxx-xxxx-xxxx-xxxx-xxxxxxxx",
"globalId": "xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx",
"id": "12345687",
"objectTypeAttributeId": "1235",
"objectAttributeValues": [
{
"value": "Red",
"searchValue": "Red",
"referencedType": false,
"displayValue": "Red"
}
],
"objectId": "1264533"
},

 

My query snippet:

 

#"Added Custom" = Table.AddColumn(#"Expanded Column", "Name", each [attributes]{1}[objectAttributeValues]{0}[value]),

 

Result : "Red" (but need this value based on objectTypeAttributeId)

 

1 is hard-coded. I believe this has to be replaced with <"objectTypeAttributeId": "1235"> condition. So that the value will be fetched properly irrespective of the placement in the response.

I am not sure how to do that. 

 

Please let me know if you need more details.

 

Thank you!

 

1 ACCEPTED SOLUTION
ZhangKun
Super User
Super User

A complete test code, please pay attention to the comments:

let
    data = Json.Document("{
        ""attributes"": [
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345678"",
                ""objectTypeAttributeId"": ""1234"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""ABCD-1264"",
                        ""searchValue"": ""ABCD-1264"",
                        ""referencedType"": false,
                        ""displayValue"": ""ABCD-1264""
                    }
                ],
                ""objectId"": ""1264533""
            },
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345687"",
                ""objectTypeAttributeId"": ""1235"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""Red"",
                        ""searchValue"": ""Red"",
                        ""referencedType"": false,
                        ""displayValue"": ""Red""
                    }
                ],
                ""objectId"": ""1264533""
            }
        ]
    }"),
    data2 = Json.Document("{
        ""attributes"": [
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345678"",
                ""objectTypeAttributeId"": ""1234"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""ABCD-1264"",
                        ""searchValue"": ""ABCD-1264"",
                        ""referencedType"": false,
                        ""displayValue"": ""ABCD-1264""
                    }
                ],
                ""objectId"": ""1264533""
            }
        ]
    }"),
    #"Expanded Column" = #table({"Col"}, {{data}, {data2}}),
    #"Added Custom" = Table.AddColumn(
        #"Expanded Column", 
        "Name", 
        // Replace [Col] with the actual column name
        each List.Select([Col][attributes], each [objectTypeAttributeId] = "1235"){0}?[objectAttributeValues]?{0}?[searchValue]?
    )
in
    #"Added Custom"

 

View solution in original post

6 REPLIES 6
ZhangKun
Super User
Super User

A complete test code, please pay attention to the comments:

let
    data = Json.Document("{
        ""attributes"": [
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345678"",
                ""objectTypeAttributeId"": ""1234"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""ABCD-1264"",
                        ""searchValue"": ""ABCD-1264"",
                        ""referencedType"": false,
                        ""displayValue"": ""ABCD-1264""
                    }
                ],
                ""objectId"": ""1264533""
            },
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345687"",
                ""objectTypeAttributeId"": ""1235"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""Red"",
                        ""searchValue"": ""Red"",
                        ""referencedType"": false,
                        ""displayValue"": ""Red""
                    }
                ],
                ""objectId"": ""1264533""
            }
        ]
    }"),
    data2 = Json.Document("{
        ""attributes"": [
            {
                ""workspaceId"": ""xxx-xxxx-xxxx-xxxx-xxxxxxxx"",
                ""globalId"": ""xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx"",
                ""id"": ""12345678"",
                ""objectTypeAttributeId"": ""1234"",
                ""objectAttributeValues"": [
                    {
                        ""value"": ""ABCD-1264"",
                        ""searchValue"": ""ABCD-1264"",
                        ""referencedType"": false,
                        ""displayValue"": ""ABCD-1264""
                    }
                ],
                ""objectId"": ""1264533""
            }
        ]
    }"),
    #"Expanded Column" = #table({"Col"}, {{data}, {data2}}),
    #"Added Custom" = Table.AddColumn(
        #"Expanded Column", 
        "Name", 
        // Replace [Col] with the actual column name
        each List.Select([Col][attributes], each [objectTypeAttributeId] = "1235"){0}?[objectAttributeValues]?{0}?[searchValue]?
    )
in
    #"Added Custom"

 

Thank you @ZhangKun. It worked! 

bhanu_gautam
Super User
Super User

@gsara45 , Try using

#"Added Custom" = Table.AddColumn(#"Expanded Column", "Name", each
let
attributes = [attributes],
filteredAttributes = List.Select(attributes, each _[objectTypeAttributeId] = "1235"),
firstMatch = if List.Count(filteredAttributes) > 0 then filteredAttributes else null,
value = if firstMatch <> null then firstMatch[objectAttributeValues][value] else null
in
value
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Thank you @bhanu_gautam for your resonse.

 

I am getting below error while using your code.

Expression.Error: We cannot apply field access to the type List.
Details:
Value=[List]
Key=objectAttributeValues

 

I missed you mention in my previous post that this JSON response has lot attributes like I have mentioned. I just posted for one such instance.

Meaning that there is one [objectTypeAttributeId] = "1235" for the all attributes listed. Just that we not sure about its position within one attribute list.

 

Here is my full code - hope this will be more clearer

 

let
url = "https://api.atlassian.com/jsm/assets/workspace/bcc5e987-a7e5-8398-8cc7-ea05j54479e9/v1/object/aql?st...",

body = "{""qlQuery"": ""objectType = Servers""}",

Source = Json.Document(Web.Contents(
url,[
Headers=[#"Authorization"="Basic <emailaddress:API token>",#"Content-Type"="application/json"],
Content=Text.ToBinary(body)
]
)
),
values = Source[values],
#"Converted to Table" = Table.FromList(values, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"objectKey", "attributes"}, {"objectKey", "attributes"}),
#"Added Custom" = Table.AddColumn(#"Expanded Column", "Server", each
let
attributes = [attributes],
filteredAttributes = List.Select(attributes, each _[objectTypeAttributeId] = "1235"),
firstMatch = if List.Count(filteredAttributes) > 0 then filteredAttributes else null,
value = if firstMatch <> null then firstMatch[objectAttributeValues][value] else null
in
value)
in
#"Added Custom"

 

powerbi.jpg

 

@gsara45 , Use this

let
url = "https://api.atlassian.com/jsm/assets/workspace/bcc5e987-a7e5-8398-8cc7-ea05j54479e9/v1/object/aql?st...",
body = "{""qlQuery"": ""objectType = Servers""}",
Source = Json.Document(Web.Contents(
url, [
Headers = [#"Authorization" = "Basic <emailaddress:API token>", #"Content-Type" = "application/json"],
Content = Text.ToBinary(body)
]
)),
values = Source[values],
#"Converted to Table" = Table.FromList(values, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"objectKey", "attributes"}, {"objectKey", "attributes"}),
#"Added Custom" = Table.AddColumn(#"Expanded Column", "Server", each
let
attributes = [attributes],
filteredAttributes = List.Select(attributes, each _[objectTypeAttributeId] = "1235"),
firstMatch = if List.Count(filteredAttributes) > 0 then filteredAttributes else null,
value = if firstMatch <> null then firstMatch[objectAttributeValues][value] else null
in
value
)
in
#"Added Custom"




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Thanks again!

 

I am getting the same error.

Moreover, I am not sure what was the change you did in my code. Can you please highlight ?

 

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

Check out the March 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.