Home > OS >  Jackson ObjectMappper Lowercase Strategy Not Working
Jackson ObjectMappper Lowercase Strategy Not Working

Time:10-07

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;
   }
}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related