Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP to JSON

naotoxxx
Participant
2,459

Hello , i'm triying to convert an internal table that i fill by myselft and based on many structures and deep structures all created by me (SE11).

Reading this: https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer

and use this:

LV_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = it_main_tb COMPRESS = ABAP_TRUE pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

But is not formating with camelCase

lv_json=[{"businesspartnercategory":"2","businesspartnergrouping":"ZNAC","organizationbpname1":"RAZÓN SOCIAL","organizationbpname2":"A","organizationbpname3":"B","organizationbpname4":"C","toBusinesspartneraddress":{"cityname":"SANTIAGO","country":"CL","district"...

and i need it like this:

{
  "d": {
    "BusinessPartnerCategory": "2",
    "BusinessPartnerGrouping": "ZNAC",
    "OrganizationBPName1": "RAZON SOCIAL",
    "OrganizationBPName2": "A",
    "OrganizationBPName3": "B",
    "OrganizationBPName4": "C",
    "SearchTerm1": "",
    "to_BusinessPartnerAddress": {
      "results": [
        {
          "CityName": "SANTIAGO",
          "Country": "CL",
          "District": "02",
          "Language": "ES",
          "PostalCode": "8320000",
          "StreetName": "HOLANDA 99",
          "to_EmailAddress": {
            "results": [
              {
                "EmailAddress": "email@email.com"
              }
            ]
          }
        }
      ]
    },

So all "_" are deleted and is not formating with cameCase

What is wrong with my class call ? any ideas?

4 REPLIES 4

keremkoseoglu
Contributor
985

This might be obvious, but in the worst case, you can build the JSON text using Concatenate (or brackets); keeping any case rule you want. If you pick this path, the codebase here might help you a little: https://github.com/keremkoseoglu/ABAP-Library/blob/master/zcl_bc_json_toolkit.abap

Tomas_Buryanek
Active Contributor
980

Do you have field names in SE11 structures with underscores?
Like this: business_partner_category = businessPartnerCategory

Your code looks OK otherwise. So if you have field names correct (with underscores) then you probably need to update /UI2/CL_JSON. I think you have some old version with a bug. There are SAP NOTES with updates...
We are using this class a lot and it works great and is very helpful.
EDIT: note that for first letter in capital you need to have field name starting with underscore:
_test_field_name (fieldname) = TestFieldName (in JSON)

-- Tomas --

0 Kudos
980

Yeap i noticed that i need a underscore before the letter that i want in capital , but thanks! 😄 the problem it's when my fields names are too long but i solve that with a replace all hehe

abo
Active Contributor
404

Thanks Tomas, this old answer pointed me in the right direction: the system I was working on indeed has such an old release that the standard /UI2/CL_JSON does not properly support camelCase and the Notes seem to be for much newer releases.

Fortunately, SAP provides a replacement, abap-to-json, which works beautifully!