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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ChadC
Frequent Visitor

Check if JSON Attribute Exists

Hi, 

 

I'm working with a paginated API. The initial fetch will return a JSON payload like:

 

{
  "data": [
   ...
  ],
  "links": {
    "next": "https://api.outreach.io/api/v2/prospects?page[limit]=50&page[offset]=200"
  }
}

My issue arises on the final fetch, where no "links" attribute exists. How can I check if this attribute exists and handle the scenario in which it inevitably does not?

 

I've tried writing the following in M with no success. I get the message: "Details: "The field 'next' of the record wasn't found.""

GetPage = (Index, Scope) =>
    let
        prevIndex = Index - 1,
        limit = Number.ToText(LimitPerPage),
        offset = Number.ToText(prevIndex * LimitPerPage),
        JSON = GetJSON(api_source_uri & Scope & "?page[limit]=" & limit & "&page[offset]=" & offset),
        Value = JSON,
        JSON_Links = if JSON[links] <> null then GetJSON(JSON[links][next]) else null,
        Output = if JSON_Links <> null then JSON_Links[data] else []
    in
        Output;

Any help is appreciated. 

 

Thanks!

1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

I'd try to use

try ... otherwise ...

syntax to catch errors, see more here
https://blog.gbrueckl.at/2013/12/error-handling-in-power-query/



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

3 REPLIES 3
Stachu
Community Champion
Community Champion

I'd try to use

try ... otherwise ...

syntax to catch errors, see more here
https://blog.gbrueckl.at/2013/12/error-handling-in-power-query/



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

ChadC
Frequent Visitor

Thanks @Stachu , that was what I was looking for!

 

Updated function:

GetPage = (Index as number, Scope as text, Offset as number) =>
    let
        // Calculate the offset. 
        _offset = Number.ToText(Index * LimitPerPage + Offset),
        // Try to fetch a JSON payload. If an error occurs, return a JSON document with an empty "data" attribute.
        JSON = try GetJSON(api_source_uri & Scope & "?page[limit]=" & strLimitPerPage & "&page[offset]=" & _offset) otherwise Json.Document("{""data"": []}"),
        // Get the ""data"" attribute of the JSON document.
        Page = JSON[data]
    in
        Page;
v-xicai
Community Support
Community Support

Hi @ChadC ,

 

If you still have this issue for Power BI, you'd better create a support ticket in Power BI Support to get further help.

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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