JSON fields and Class properties - are they case sensitive?
Hi noble devs!
Just building a simple frontend->JSON->IRIS backend story, and figured that IRIS while importing via %JSON.Adaptor wants JSON fields to match property names, meaning even should match the case. Like:
{ name: "John",
surname: "Doe"}will have issues while saving the dynamic object to a class Sample.Person with:
Class Sample.Person Extends (%Persistent, %JSON.Adaptor)
{
Property Name: %Sting;Property Surname: %String;
}As soon as the case is not the same...
Is there any setting to fix this?) By fixing I mean I want IRIS to don't care about the case JSON fields, as it doesn't matter from SQL point of view, .e.g., IRIS will not allow two same properties but different case:
Property Name : %String;Property name: %String;Thoughts?
Comments
To ensure InterSystems IRIS does not care about the case sensitivity of JSON field names when using %JSON.Adaptor, you can utilize the %JSONFIELDNAME parameter. This parameter allows you to define an alias for a property name, mapping it explicitly to the corresponding JSON field name. For example:
Class Sample.Person Extends (%Persistent, %JSON.Adaptor)
{
Property Name As %String(%JSONFIELDNAME = "name");
Property Surname As %String(%JSONFIELDNAME = "surname");
}
With this configuration, the fields in JSON { "name": "John", "surname": "Doe" } will correctly map to the properties Name and Surname in the class without any case sensitivity issues [1][2].
Sources:
Yes, thank you, AI. But I want it not to care about the case completely.
Hello @Evgeny Shvarov
Unfortunately, when the %JSONFIELDNAME parameter is not explicitly defined, the JSON adaptor treats the property name as a constant, making it fixed and case-sensitive.
However, you can define a custom mapping using an XData block to control how properties are mapped to JSON fields. This provides greater flexibility, and you can even use both %JSONFIELDNAME and XData mappings within the same class.
Example: Custom Mapping with XData
XData OnlyLowercaseTopLevel
{
<Mappingxmlns="http://www.intersystems.com/jsonmapping"><PropertyName="Name"FieldName="eventName"/><PropertyName="Location"Include="INPUTONLY"/></Mapping>
}Do obj.%JSONImport(dao)Default import (uses standard property names)Do obj.%JSONImport(dao, "OnlyLowercaseTopLevel")Import using a custom mapping
Thank you, @Ashok Kumar T ! I went to submit an idea to fix this, and found your's one :) Voted.
Thank you!
You probably have a special use case but, in general, I don't think it makes a lot of sense because:
- Class properties ARE Case-sensitive by its nature (contrary to your statement in Ideas portal)
- JSON fields ARE Case-sensitive by its nature (as per JSON standard)
Thank you, @Enrico Parisi ! But are you sure about 1.?
I've just created a class:
Class dc.sample.ObjectScript
{
property Name As%String;property name as%String;
}}
And have a compilation error:
ERROR! In class 'dc.sample.ObjectScript' Property 'Name' from 'dc.sample.ObjectScript' and 'name' from 'dc.sample.ObjectScript' have the same name but differ in case.
Yes, pretty sure, that is what is taught in the ISC training courses and what the documentation says:
Description ($PROPERTY)
Property names are case-sensitive
Selecting Fields
Field names in a SELECT statement are not case-sensitive. SqlFieldName names and property names are case-sensitive.
Rules for Class Member Names
Note that the system preserves the case that you use when you define classes, and you must exactly match the case as given in the class definition. However, two class members cannot have names that differ only in case. For example, the identifiers id1 and ID1 are considered identical for purposes of (uniqueness.)
Right. So, what is the reason, or if you may, what are the benefits of properties to be case sensitive?
Properties are case sensitive.
What would be the use case for that?
How do you mean "use case"? You just shouldn't bother about the case of property names in IRIS while sending a JSON from your frontend to the IRIS REST-API. Like you don't care while sending it vs any SQL engine backend.
As a point of clarification, property names ARE case sensitive in ObjectScript, but in SQL, column names ARE NOT case sensitive. You aren't allowed have a property named "name" and a property named "Name" because they would be ambiguous when doing SQL queries.
Exactly. And as I mentioned above, you cannot compile such a class with properties that differ only in case. What are the benefits of properties being case sensitive? Don't see any.
For me some good reasons could be performance (reading & writing internal storage without conversions), the interoperability with other programming languages where identifiers are case sensitive (e.g. C, C++, Java, C#, Python etc.) and that most unix based file systems are case sensitive too.
Thank you, Kai! Do you have any examples where the case-sensitive principle can be used, e.g., with C++ or C# and InterSystems ObjectScript class properties?