Brady Shober
Brady Shober

Reputation: 67

Jackson ObjectMappper Lowercase Strategy Not Working

I am writing a process that needs to pull JSON data from an API and provide it to another system that requires the field names to be completely lowercased. I have attempted to utilize the built in LowerCaseStrategy but this does not work. An example of what I have tried is:

package com.example

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

public class Example {
   private static final ObjectMapper mapper = new ObjectMapper();

   public Example(){
      mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.LowerCaseStrategy());
   }

   public JsonNode fetchData(String url) throws MalformedURLException, IOException{
      JsonNode data = mapper.readTree(new URL(url));
      return data;
   }
}

Upvotes: 0

Views: 1068

Answers (2)

Brady Shober
Brady Shober

Reputation: 67

I was able to find the following gist which is working for me https://gist.github.com/stmcallister/92d0b4c2355a490ffed008cfbda69063

There's probably a better solution out there but this is perfect for my use case.

Upvotes: 0

Tim Moore
Tim Moore

Reputation: 9472

A PropertyNamingStrategy affects how JSON property names are mapped from methods and fields in a Java class. This code doesn't do any mapping to Java objects, it only deserializes JSON into a JsonNode. In that case, PropertyNamingStrategy doesn't apply, and the names are retained from the original JSON source.

Upvotes: 1

Related Questions