JSONArray bug
Found an issue where i would create a JSONArray object then push JSONObjects to it, then ultimately serialize in the end. It would work, but further down the code where i want to again parse a whole new json string using JSONObject.parse, I would get a "A property already exists with the name: " even though there was only one property with that name.
Example code that broke:
set JSONLDArray = new JSONArray 'SEEMS TO BE THE PROBLEMATIC LINE
set JSONLDBreadcrumbItem = New JSONobject
JSONLDBreadcrumbItem.add "@type","ListItem"
JSONLDBreadcrumbItem.add "position",1
JSONLDBreadcrumbItem.add "name","Home"
JSONLDBreadcrumbItem.add "item",siteurl
JSONLDArray.push JSONLDBreadcrumbItem
set JSONLDBreadcrumbItem = nothing
set rsBreadcrumb = conn.Execute(SQL)
if not rsBreadcrumb.EOF then
BCProductPageID = rsBreadcrumb("ProductPageID")
BCProductName = rsBreadcrumb("ProductName")
BCWebmodulenumber = rsBreadcrumb("WebModuleRefID")
if isNull(BCWebmodulenumber) then BCWebmodulenumber = 0
BCDepartmentName = rsBreadcrumb("depz")
BCCategoryName = rsBreadcrumb("catnamez")
BCTypeName = rsBreadcrumb("typex")
BCDepartmentID = rsBreadcrumb("ProductDepartmentID")
BCCategoryID = rsBreadcrumb("ProductCategoryID")
BCTypeID = rsBreadcrumb("ProductTypeID")
set JSONLDBreadcrumbItem = New JSONobject
JSONLDBreadcrumbItem.add "@type","ListItem"
JSONLDBreadcrumbItem.add "position",2
JSONLDBreadcrumbItem.add "name",BCDepartmentName
JSONLDBreadcrumbItem.add "item",pageurl&"?cid="&BCDepartmentID&"&c1="&BCDepartmentName&"&N="&BCWebmodulenumber
JSONLDArray.push JSONLDBreadcrumbItem
set JSONLDBreadcrumbItem = nothing
end if
rsBreadcrumb.close
set rsBreadcrumb = nothing
set JSONLDBreadcrumbList = New JSONobject
JSONLDBreadcrumbList.add "@context","https://schema.org"
JSONLDBreadcrumbList.add "@type","BreadcrumbList"
JSONLDBreadcrumbList.add "itemListElement",JSONLDArray
response.write("<script type=""application/ld+json"">" & Replace(Replace(JSONLDBreadcrumbList.serialize(),"\/\/","//"),"\/","/") & "</script>")
set JSONLDBreadcrumbList = nothing
Set obj = new JSONobject
obj.Parse("{""test"":[{""hello"":""world""},{""abc"":123}]}") 'IT BREAKS HERE WITH ERROR "A property already exists with the name: test."
Found a workaround for now by replacing line 1 with:
set JSONLDArray = new JSONobject
JSONLDArray.parse("{""data"":[]}")
set JSONLDArray = JSONLDArray.value("data")
Hi @armysarge,
Thanks for bringing this up. I tried to reproduce the issue on my local machine and I couldn't. Since I don't have access to your DB sample, I simplified your example code above to remove the recordset and set string properties and it doesn't yield any error:
<%
set JSONLDArray = new JSONArray
set JSONLDBreadcrumbItem = New JSONobject
JSONLDBreadcrumbItem.add "@type","ListItem"
JSONLDBreadcrumbItem.add "position",1
JSONLDBreadcrumbItem.add "name","Home"
JSONLDBreadcrumbItem.add "item",siteurl
JSONLDArray.push JSONLDBreadcrumbItem
set JSONLDBreadcrumbItem = nothing
' set rsBreadcrumb = conn.Execute(SQL)
' if not rsBreadcrumb.EOF then
BCProductPageID = "ProductPageID"
BCProductName = "ProductName"
BCWebmodulenumber = "WebModuleRefID"
if isNull(BCWebmodulenumber) then BCWebmodulenumber = 0
BCDepartmentName = "depz"
BCCategoryName = "catnamez"
BCTypeName = "typex"
BCDepartmentID = "ProductDepartmentID"
BCCategoryID = "ProductCategoryID"
BCTypeID = "ProductTypeID"
set JSONLDBreadcrumbItem = New JSONobject
JSONLDBreadcrumbItem.add "@type","ListItem"
JSONLDBreadcrumbItem.add "position",2
JSONLDBreadcrumbItem.add "name",BCDepartmentName
JSONLDBreadcrumbItem.add "item",pageurl&"?cid="&BCDepartmentID&"&c1="&BCDepartmentName&"&N="&BCWebmodulenumber
JSONLDArray.push JSONLDBreadcrumbItem
set JSONLDBreadcrumbItem = nothing
' end if
' rsBreadcrumb.close
' set rsBreadcrumb = nothing
set JSONLDBreadcrumbList = New JSONobject
JSONLDBreadcrumbList.add "@context","https://schema.org"
JSONLDBreadcrumbList.add "@type","BreadcrumbList"
JSONLDBreadcrumbList.add "itemListElement",JSONLDArray
response.write("<script type=""application/ld+json"">" & Replace(Replace(JSONLDBreadcrumbList.serialize(),"\/\/","//"),"\/","/") & "</script>")
set JSONLDBreadcrumbList = nothing
Set obj = new JSONobject
obj.Parse("{""test"":[{""hello"":""world""},{""abc"":123}]}")
%>
That error is caused by trying to create a property that already exists in the object. It can be triggered by parsing a JSON string with duplicate keys as well.
Your workaround shouldn't make a difference, as the result will be the same, an empty array object.
Can you try to reproduce the issue with my sample above?