How to deal with nullable values in SAP?

This space is dedicated to SAP users - administrators and programmers who want to integrate PTV Developer (native), the PTV xServers or the SAP Hana Spatial Services (HSS). Topics which require specific SAP based knowhow or which deal with SAP based mechanisms will be collected here.
Post Reply
User avatar
Bernd Welter
Site Admin
Posts: 3014
Joined: Mon Apr 14, 2014 10:28 am
Contact:

How to deal with nullable values in SAP?

Post by Bernd Welter »

Hi there,

one of my counterparts wants to fill a JSON request body and needs to work with nullable elements:
He runs into this message:

Code: Select all

{
  "faultInfo": {
    "$type": "ParameterConflictFault",
    "hint": "Either change the calculation mode or remove the tweaks to objective from the plan tours options.",
    "parameterA": "/planToursRequest/planToursOptions/@tweaksToObjective",
    "parameterB": "/planToursRequest/planToursOptions/@calculationMode",
    "valueA": "not null",
    "valueB": "CUSTOM"
  },
  "message": "To use calculation mode CUSTOM the tweaks to objective may not be set."
}
To use calculation mode CUSTOM the tweaks to objective may not be set.
In the JSON this should look like in the example when it comes to "tweaksToObjective" or "customCalculationModeConfiguration".

Code: Select all

  "planToursOptions": {
    "tweaksToObjective": null,
    "considerOrderPriorities": true,
    "calculationMode": "QUALITY",
    "customCalculationModeConfiguration": null
    "planningHorizon": {
      "$type": "StartEndInterval",
      "start": "2025-10-19T22:00:00+02:00",
      "end": "2025-10-20T21:59:59+02:00"
    },
    "restrictions": { "mixedLoadingProhibitions": [] }
  }
So such a property should appear as
  • "theProperty" : null
  • not at all
Looks like this isn't that easy in SAP.
Can anyone assist?

Bernd
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Joost
Posts: 317
Joined: Fri Apr 25, 2014 1:46 pm

Re: How to deal with nullable values in SAP?

Post by Joost »

Both options should be valid.
Joost Claessen
Senior Technical Consultant
PTV Benelux
User avatar
Bernd Welter
Site Admin
Posts: 3014
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: How to deal with nullable values in SAP?

Post by Bernd Welter »

ok, I haven't been clear enough...
This is not about the style of the message - it is about how to tell SAP to produce the proper style...
Important to understand - as mentioned in the error:
If you want to use a custom blob (CalculationMode="CUSTOM") you are supposed to not set the tweaks...
Technically a property can be used by the following styles:
"tweaksToObjective": { ...content... }runs into the error - not part of this thread, the client knows how to populate a property ;-), /td]http-400
"tweaksToObjective": {}Is not NULL but an object which has the properties with their default values: avoidIntersections=false, minimizeNumberOfTours=true and therefore creates the errorhttp-400
"tweaksToObjective": nullworks as designedhttp-200
"tweaksToObjective" doesn't appear in the request works finehttp-200
Now my client wants to achieve one of the http-200, but doesn't know how to tell SAP to run into this property style.

Better?
JSON wrote:{
"faultInfo": {
"$type": "ParameterConflictFault",
"hint": "Either change the calculation mode or remove the tweaks to objective from the plan tours options.",
"parameterA": "/planToursRequest/planToursOptions/@tweaksToObjective",
"parameterB": "/planToursRequest/planToursOptions/@calculationMode",
"valueA": "not null",
"valueB": "CUSTOM"
},
"message": "To use calculation mode CUSTOM the tweaks to objective may not be set."
}
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
Bernd Welter
Site Admin
Posts: 3014
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: How to deal with nullable values in SAP?

Post by Bernd Welter »

:idea: Here's what Microsoft 365 Copilot says ;-)
:!: If you are evaluating to apply one of these approaches be aware that I cannot assist- it is at your own risk!
This is a common challenge when integrating SAP systems with external services via JSON-based APIs. SAP’s serialization behavior—especially when using ABAP structures or classes—can indeed result in empty objects ({}) being serialized instead of null or omitting the property entirely.
Here are a few approaches your client can explore, depending on their SAP setup (e.g., using ABAP, SAP PI/PO, or SAP CPI):

🔧 Option 1: Force null in JSON Output
If the goal is to explicitly send "tweaksToObjective": null, the ABAP structure or class must have the field set to INITIAL (i.e., not instantiated) and the JSON serializer must be configured to include nulls.
In ABAP:

Code: Select all

ABAPDATA: ls_payload TYPE zmy_structure.
CLEAR ls_payload-tweaksToObjective. " Ensures it's initial (null)
Then, when serializing:

Code: Select all

ABAPDATA(lo_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id  
  SOURCE mydata = ls_payload  
  RESULT XML lo_writer.Show more lines
But this may still omit the field entirely unless you use a custom serializer or enhancement.

🧼 Option 2: Omit the Property Entirely
If the server prefers the property to be absent rather than null, then the field should be left unpopulated and the serializer should be configured to skip initial fields.
In ABAP, this is the default behavior for many serializers. However, if a nested structure is instantiated (even if empty), it will be serialized as {}.
So:

Code: Select all

ABAPCLEAR ls_payload-tweaksToObjective. " Do not instantiate it
Avoid:

Code: Select all

ABAPCREATE DATA ls_payload-tweaksToObjective. " This leads to {}
🛠️ Option 3: Use Custom JSON Serialization
If SAP’s standard serialization doesn’t meet the requirement, your client can:
  • Use cl_trex_json_serializer or cl_fdt_json for more control.
  • Manually build the JSON string using string templates or cl_sxml_string_writer.
Example:

Code: Select all

ABAPDATA(json_string) = |{{ "tweaksToObjective": null, "otherField": "{ ls_payload-otherField }" }}|.
✅ Best Practice
  • Document the API contract: Ensure the receiving system clearly defines whether null or omission is preferred.
  • Test with Postman or curl: Simulate both cases to confirm server behavior.
  • Log the serialized JSON: To verify what SAP is actually sending.
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Post Reply