jsongroovyjsonbuilder

Groovy JsonBuilder capitalising field names


I'm using the Groovy JsonBuilder to generate JSON to send over HTTP. My problem is it's capitalising some of the keys in the map it's given.

I give it an object of this class:

public class TestSNP {
    private String snpID;

    TestSNP(String input) {
        snpID = input.split("\\s+")[1];
    }

    String getSNPID() {
        return snpID;
    }
}

This is the test that fails:

import groovy.json.*

class Test {
    @Test
        void jsonBuilderTest() {
            def testSNP = new TestSNP("1 rs444444 2 3")
            assert new groovy.json.JsonBuilder(testSNP).toString() == '{"snpID":"rs444444"}'
        }
}

I get

{"SNPID":"rs444444"}

instead of

{"snpID":"rs444444"}

(this is a simplified example demonstrating my problem)


Solution

  • Change:

    String getSNPID() {
        return snpID;
    }
    

    to:

    String getSnpID() {
        return snpID;
    }
    

    And it will work as you expect