如何解决JSON-LD中的“Duplicate key found”错误?
Fixing "JSON-LD Duplicate key found" for Schema.org Opening Hours
Hey there, let's tackle that duplicate key error you're seeing in your opening hours JSON-LD. The root issue here is super straightforward: JSON doesn't allow duplicate keys in the same object, so when you repeat opens, closes, or daysOfWeek inside a single openingHoursSpecification block, the parser throws an error.
The Correct Schema.org Approach
Schema.org expects each distinct time slot (even for the same day) to be its own openingHoursSpecification object, wrapped in an array. Here's how to refactor your code properly:
Example of the Wrong (Error-Causing) Code:
{ "@context": "https://schema.org", "@type": "LocalBusiness", "name": "Your Office", "openingHoursSpecification": { "daysOfWeek": "Monday", "opens": "09:00", "closes": "12:30", "daysOfWeek": "Monday", "opens": "13:30", "closes": "18:00", "daysOfWeek": "Saturday", "opens": "10:00", "closes": "15:00" } }
Refactored (Error-Free) Code:
{ "@context": "https://schema.org", "@type": "LocalBusiness", "name": "Your Office", "openingHoursSpecification": [ // Monday morning slot { "daysOfWeek": "Monday", "opens": "09:00", "closes": "12:30" }, // Monday afternoon slot { "daysOfWeek": "Monday", "opens": "13:30", "closes": "18:00" }, // Weekdays (Tue-Fri) with same hours { "daysOfWeek": ["Tuesday", "Wednesday", "Thursday", "Friday"], "opens": "09:00", "closes": "18:00" }, // Saturday slot { "daysOfWeek": "Saturday", "opens": "10:00", "closes": "15:00" } ] }
Key Takeaways to Avoid Duplicate Keys:
- Use an array for
openingHoursSpecification: Wrap all your time slot objects in square brackets[]—this tells Schema.org you're defining multiple distinct opening periods. - Unique objects per time slot: Even if it's the same day with split hours (like morning/afternoon breaks), each interval gets its own object with one set of
daysOfWeek,opens, andcloses. - Group identical weekdays: If multiple days share the same hours, use an array for
daysOfWeek(e.g.,["Tuesday", "Wednesday"]) to avoid repeating objects unnecessarily. - Stick to 24-hour time: Use consistent formatting like
09:00instead of9 AMto ensure compatibility with Google's parser.
Once you refactor your code this way, run it through the structured data testing tool again—those duplicate key errors should be gone.
内容的提问来源于stack exchange,提问作者VBMali
相关产品推荐
相关产品推荐

