You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JS移除JSON空值键?Google Sheets报错求助

How to Clean JSON by Removing Keys with Empty Values (Including Nested Entries) for API Calls in Google Sheets?

Hey everyone, I'm working on preparing JSON data for an API request in Google Sheets, and I need to strip out any keys that have empty values—including handling nested objects where an entire parent field should be skipped if any of its nested values are empty.

Here's the raw JSON I'm starting with:

{ 
  "top": [
    { "language": "English", "value": "" }, 
    { "language": "German", "value": "hASTA" }
  ], 
  "bottom": [
    { "language": "English", "value": "jfgfjg" }, 
    { "language": "German", "value": "fkjhf" }
  ], 
  "In": "12 am", 
  "Out": "3 am", 
  "Type": "" 
}

My goal is to only keep fields that have no empty values at all. For example, since the top array contains an object with an empty value, I need to exclude the entire top key. Similarly, the Type field has an empty string, so it should be removed too. The final cleaned JSON should look like this:

{ 
  "bottom": [
    { "language": "English", "value": "jfgfjg" }, 
    { "language": "German", "value": "fkjhf" }
  ], 
  "In": "12 am", 
  "Out": "3 am" 
}

I tried using this simple filter function, but it throws an error in Google Sheets:

apidata = userdata.filter(function(x) { return x !== "" });

I know the filter method works on arrays, but my data is an object with nested arrays, so that's probably why it's failing. Can anyone walk me through the correct way to handle this in Google Sheets' Apps Script?

内容的提问来源于stack exchange,提问作者colen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:52:52