Send empty object if json string is null

I’m trying to detect if a JSON string exists (i.e. not null) and send an empty json object {} instead.

First, I use Transform to JSON to turn an object into JSON

Then, I use an if statement to check if the object exists or not

But it’s still being sent as null

PS, I’ve tried using three different approaches to detect null: null, "null" and backticks.

Hello,

can you try with emptystring in your comparison (instead of null) to see if it works?

image

Benjamin

2 Likes

Thanks for the suggestion, but that didn’t work. I’ve also tried {{ifempty(8.json; "{}")}}, but this also didn’t work.

Hmm, I don’t think you can validate the JSON directly, because it either outputs a valid JSON or throws an error.

If it throws an error, you can simply use a Resume directive to return {} (empty JSON object string).

If you need further assistance, please provide the following:

  • An example of invalid “4.timeSpend.timePerWeek” variable (the output bundle of module [4])

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

I’m not getting any errors on the Transform to JSON step. It’s either returning valid JSON, or null as shown in this image.

It’s not the end of the world, I’ll just need to check for null at the other end.

Since the output is “null” (a string), not null (a special variable with pink background),

Then you should convert the null in your third screenshot above to “null” (text WITHOUT pink background).

This image’s null should be manually typed in as “null” (without double quotes).

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

I’ve tried each of the following, but none work.

I was able to make it work with the inverse:

Output 1
Screenshot_2024-06-19_200616

Output 2
Screenshot_2024-06-19_200637

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

I will give that a try, thank you. Annoyingly, my dataset now doesn’t have any null values, so I’ll need to wait until I get some.

I can confirm at the very least that the json string is coming though correctly still, however, so I"ve got a good feeling about this one. It’s weird if it works this way around, but not the other.

Thank you for your help.