# Change capitalized field names to camelCase (DRF Serializer)

**URL:** https://forum.djangoproject.com/t/change-capitalized-field-names-to-camelcase-drf-serializer/7984
**Category:** Using Django
**Created:** [May 21, 2021, 12:35am UTC](https://forum.djangoproject.com/t/change-capitalized-field-names-to-camelcase-drf-serializer/7984 "2021-05-21T00:35:30Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)
#### Post date: [May 21, 2021, 12:35am UTC](https://forum.djangoproject.com/t/change-capitalized-field-names-to-camelcase-drf-serializer/7984/1 "2021-05-21T00:35:30Z")

</div>

Hello 👋

I’m consuming an API that returns a JSON object with capitalized field names like this.

```auto
{
  "CODE": 1,
  "PERSONNAME": "Marco",
  "PERSONSURNAME": "Richetta"
}

```

I created a serializer to receive and validate this data

```auto
class PersonSerializer(serializers.Serializer):

    CODE = serializers.IntegerField()
    PERSONNAME = serializers.CharField()
    PERSONSURNAME = serializers.CharField()

```

### Problem

I need to change the field names to `camelCase` but I’m not sure where is the best place to do it.

At the moment I overwrote the `to_representation` method of the serializer like this:

```py
    def to_representation(self, instance):

        # This map is used to transform the field names
        fields_map = {
            "CODE": "code",
            "PERSONNAME": "personName",
            "PERSONSURNAME": "personSurname",
        }

        for key, value in fields_map.items():
            # Field name is changed. Ej: PERSONNAME => personName
            instance[value] = instance.pop(key)

        return instance

```

It does the job but I’m not sure it’s safe to do it there.

There’s a library called [djangorestframework-camel-case](https://github.com/vbabiy/djangorestframework-camel-case), that converts `underscore <=> camelCase` with some helper functions to determine how to make the conversion. In my case I can’t see a pattern so I need to define the `fields_map` based on the fields the API gives me.

### Swagger problem

I have another problem with Swagger showing the example Response with the field names also capitalized.  
It seems it inspects information of the serializer fields from another method, but I’m still looking for that.  
The library I’m using for Swagger generation is [drf-yasg]

Thanks in advance.
