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
jatpola
Frequent Visitor

Error handling: JSON field can either be logical or record

Hi, I have a JSON field ("sentiment" in the screenshots ) which can either be "FALSE" or contain a Record.

If it's a Record I'd like to access it, otherwise I need some error handling (return null values, or something like that).

The first two screenshots show the two possible scenarios for the field "sentiment", and the third shows the error I'm getting (in English it would be something like "Can't apply field access to Logical type")

jatpola_0-1615217972655.png

jatpola_1-1615218136982.png

jatpola_2-1615218153987.png

 

 

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @jatpola 

You can separate the steps that come after the try ... otherwise into a function and then call it only if sentiment is not null

let

    ContinueTransforms = (level_1) =>
    let 
        level_2 = level_1[stats],
        level_3 = level_2[metrics],
        level_4 = level_3[score],
        Result = Record.ToTable(level_4)

    in Result,

    Source = API_JSON,
    level_1 = try Source[sentiment] otherwise null,

    #"To Table" = if level_1 is null then null else ContinueTransforms(level_1)
in
    #"To Table"

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

3 REPLIES 3
PhilipTreacy
Super User
Super User

Hi @jatpola 

You can separate the steps that come after the try ... otherwise into a function and then call it only if sentiment is not null

let

    ContinueTransforms = (level_1) =>
    let 
        level_2 = level_1[stats],
        level_3 = level_2[metrics],
        level_4 = level_3[score],
        Result = Record.ToTable(level_4)

    in Result,

    Source = API_JSON,
    level_1 = try Source[sentiment] otherwise null,

    #"To Table" = if level_1 is null then null else ContinueTransforms(level_1)
in
    #"To Table"

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


PhilipTreacy
Super User
Super User

Hi @jatpola 

In your query you need to add a step like this

Value = try Record.ToTable(#"PreviousStepName"{10}[Value]) otherwise null

But this willjust result in null if there is an error (becasue there's no record, just FALSE).

If you want to still have a table after an erroroccurs use this

Value = try Record.ToTable(#"PreviousStepName"{10}[Value]) otherwise #"PreviousStepName"

You'll need to insert the PreviousStepName from your query into these snippets.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thanks @PhilipTreacy , your answer was useful but I still need some help to achieve the desired output.

After accessing the 'sentiment' record I still perform some steps. (see code)

How do I avoid trying to perform those steps if "sentiment" doesn't contain data? Do I have to include a "try" & "otherwise" logic in every line of code?

Thanks a lot

let
    Source = API_JSON,
    level_1 = try Source[sentiment] otherwise null,
    level_2 = level_1[stats],
    level_3 = level_2[metrics],
    level_4 = level_3[score],
    #"To table" = Record.ToTable(level_4)
in
    #"To table"

 

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.