0

I would like to convert the bpi field of the following json string:

{
    "bpi": {
        "2017-10-15": 5697.3917,
        "2017-10-16": 5754.2213,
        "2017-10-17": 5595.235,
        "2017-10-18": 5572.1988,
        "2017-10-19": 5699.5838,
        "2017-10-20": 5984.0863,
        "2017-10-21": 6013.2288,
        "2017-10-22": 5984.9563,
        "2017-10-23": 5895.2988,
        "2017-10-24": 5518.85,
        "2017-10-25": 5733.9038,
        "2017-10-26": 5888.145,
        "2017-10-27": 5767.68,
        "2017-10-28": 5732.825,
        "2017-10-29": 6140.5313,
        "2017-10-30": 6121.8,
        "2017-10-31": 6447.6675
    },
        "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index. BPI value data returned as USD.",
        "time": {
        "updated": "Nov 1, 2017 13:49:57 UTC",
        "updatedISO": "2017-11-01T13:49:57+00:00"
    }
}

into a hashmap - preferably Map<Date, Double> but Map<String, String> will do as well (then I would have to process it later on). I read somewhere how to convert json arrays to java arrays but since the desired array is wrapped in the bpi sub field I dont know how to access it. Could you please help me with that?

Thanks a lot

1 Answer 1

1

It's pretty straight forward with Jackson. First define a class to hold the values:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {

    private Map<LocalDate, Double> bpi;

    // Getters and setters ommited
}

Then read your JSON string:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

Foo foo = mapper.readValue(json, Foo.class);

Alternatively you can use:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

JsonNode node = mapper.readTree(json).get("bpi");

ObjectReader reader = mapper.readerFor(new TypeReference<Map<LocalDate, Double>>() {});
Map<LocalDate, Double> bpi = reader.readValue(node);

The LocalDate support comes from the jackson-datatype-jsr310 module.

Add the following dependency you project:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson.version}</version>
</dependency>

And then register the JavaTimeModule in your ObjectMapper instance:

mapper.registerModule(new JavaTimeModule());

If you prefer to use Date instead of LocalDate, then you don't need any to add any dependency and you don't need to register any module either. Just use Date instead of LocalDate in the above examples. However I advise you to use LocalDate instead.

Sign up to request clarification or add additional context in comments.

2 Comments

And jackson also takes care about the conversion from String to Date / LocalDate?
@milkpirate Yes. See my updated answer. The jackson-datatype-jsr310 dependency is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.