3

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.

Arun Raja
  • 1,554
  • 16
  • 26
  • You could replace the `=` with `:` and then parse it as JSON, if that's the only problem. – Alkis Kalogeris Jun 09 '15 at 07:40
  • tried that still not working. I did change all the = to : manually. – Arun Raja Jun 09 '15 at 07:42
  • not only that. You have to change each attibute name within double quotes. For example `"securityData"`. Like that you need to replace all attribute name within double quotes. – Estimate Jun 09 '15 at 07:45
  • The biggest problem is the arrays. `fieldData[] = {...}` should be like this `fieldData = [...]`. Can you provide a link to the API? – Alkis Kalogeris Jun 09 '15 at 07:47
  • https://www.jsoneditoronline.org/ I am using this. I am trying a full trial and error method with double quotes. Still I am not getting a valid json. But whats the use of doing this if we are doing it maually. I cant be doing this for every response. – Arun Raja Jun 09 '15 at 07:48
  • You cannot use the API unless you have a bloomberg terminal. – Arun Raja Jun 09 '15 at 07:49
  • I think you're supposed to use the Bloomberg API to parse the responses instead of trying to parse them as JSON. See chapter 4 of http://www.bloomberglabs.com/api/content/uploads/sites/2/2014/07/blpapi-developers-guide-2.54.pdf . – Pieter Kuijpers Jun 09 '15 at 07:50
  • @PieterKuijpers I have seen it. But they have not given clearly of how to extract the values of it. Can you please guide me ? – Arun Raja Jun 09 '15 at 07:56
  • thanks a lot.. its working now.. able to extract individual values.. – Arun Raja Jun 09 '15 at 08:41

1 Answers1

3

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks a lot. I tried getting the values as given in the page 45 of bloomberg guide. will try this method also. – Arun Raja Jun 09 '15 at 09:25
  • page 45 if for a ReferenceDataRequest and would not work for your HistoricalDataRequest. You need to use the code in Section 7.2.2 (page 83) for that. – assylias Jun 09 '15 at 09:40