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

如何在TypeScript中动态创建带单引号键的JSON Schema对象?

Fixing Single-Quoted Keys for JSON Schema Form Builder

It sounds like your Schema Form Builder has a quirk where it expects object keys wrapped in single quotes instead of the standard JSON double quotes—even though technically JSON requires double quotes. Let’s walk through a few reliable ways to generate the format you need:

1. String Replacement (Quick Fix)

If you’re already generating a valid JSON string (with double quotes), you can use a regex to replace only the key-enclosing double quotes with single quotes. This avoids messing up any double quotes that might be present in your values.

// Assume this is your dynamically generated JSON object
const formValueData = { 
  discountauthorizationelementsname: "userroleid,metalid,purityid,discountmodeid,categoryid,discounttypeid,discountvalue,creditpercent,creditvalue", 
  discountauthorizationelementstable: "discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization", 
  discountauthorizationelementscolmn:"userroleid,metalid,purityid,discountmodeid,categoryid,discounttypeid,discountvalue,creditpercent,creditvalue", 
  discountauthorizationelementspartabname: "discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization,discountauthorization", 
  discountauthorizationviewfieldids:"110", 
  viewfieldids:"110", 
  moduleid:"110", 
  discauthpassword:"21212121212" 
};

// Convert to JSON string, then replace key quotes
const singleQuotedString = JSON.stringify(formValueData)
  .replace(/"([^"]+)":/g, "'$1':") // Replace only key-enclosing quotes
  .replace(/:/g, ": ") // Optional: add space after colon for readability
  .replace(/,/g, ", "); // Optional: add space after commas

console.log(singleQuotedString);

2. Manual String Construction (More Control)

If you want full control over the output format, build the string manually by iterating over your object’s key-value pairs. This lets you handle edge cases like single quotes in values (by escaping them) to avoid syntax errors.

function convertToSingleQuotedObj(obj) {
  return Object.entries(obj).map(([key, value]) => {
    // Escape single quotes in string values to prevent syntax breaks
    const processedValue = typeof value === 'string' 
      ? `'${value.replace(/'/g, "\\'")}'` 
      : value;
    return `'${key}': ${processedValue}`;
  }).join(', ');
}

// Generate the final formatted string
const singleQuotedResult = `{ ${convertToSingleQuotedObj(this.FormValueData)} }`;

3. Check for Tool Configuration (Long-Term Fix)

Before resorting to string hacks, double-check if your Schema Form Builder has a setting to accept standard JSON (with double quotes). Many tools that claim to require single quotes actually support JSON natively—this quirk might just be a documentation oversight.

Important Note:

If your values contain single quotes, make sure to escape them (as shown in the manual construction method) to avoid breaking the syntax of the generated string.

内容的提问来源于stack exchange,提问作者Code is Cheap Show me The Talk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:57:04