parsing the bloomberg response in Java

2.1k views Asked by At

I get the bloomberg response like this. I would like to parse this and get the values out to be put in a excel or csv.

Bloomberg response is a headache in kind-of XML response. Is there a simple way to directly parse into JSON ? (from object 'session' or from object 'event')

HistoricalDataResponse = {
    securityData = {
        security = "S X5 Comdty"
        eidData[] = {
            14001, 14001
        }
        sequenceNumber = 1
        fieldExceptions[] = {
        }
        fieldData[] = {
            fieldData = {
                date = 2015-05-06
                PX_LAST = 956.0
                OPEN = 967.25
            }
            fieldData = {
                date = 2015-06-06
                PX_LAST = 914.25
                OPEN = 956.0
            }
        }
    }
}

This is the response. Since we have "=" instead of ":" in any json online viewer it gives error as a invalid json.

1

There are 1 answers

2
assylias On BEST ANSWER

The Bloomberg API does not produce valid JSON. Although you may get away with parsing it as JSON after making some modifications it is not a robust approach as the format may vary depending on the type of query and fields you want to retrieve (and may change in the future as it is not part of the specification).

You should parse it using the provided methods as detailed in the documentation (see the example in section 7.2.2 - Historical Data Request of the developer's guide).

Alternatively you could use jBloomberg (disclaimer: I'm the author) and your code would look like this:

 BloombergSession session = new DefaultBloombergSession();
 LocalDate now = LocalDate.now();
 RequestBuilder<HistoricalData> hrb = new HistoricalRequestBuilder("S X5 Comdty",
                          Arrays.asList("PX_LAST", "OPEN"),
                          now.minusDays(7), now);

 HistoricalData result = session.submit(hrb).get();
 Map<LocalDate, TypedObject> data = result.forSecurity("SPX Index").forField("PX_LAST").get();
 for (Map.Entry<LocalDate, TypedObject> e : data.entrySet()) {
     LocalDate dt = e.getKey();
     double price = e.getValue().asDouble();
     System.out.println("[" + dt + "] " + price);
 }

More examples are available at the bottom of this page.