1
$\begingroup$

Consider the following function:

 f[json_] := URLFetch[
   "https://developers.betfair.com/api.betfair.com/exchange/betting/\
 json-rpc/v1",
   {"StatusCode", "Headers", "Content"},
   "Body" -> json,
   Method -> "POST",
   "Headers" -> {"X-Application" -> DEVKEY, 
   "X-Authentication" -> TOKEN}]

( TOKEN and DEVKEY have values but are private )

The function is called as follows:

 f["[
 {\"jsonrpc\": \"2.0\", 
 \"method\": \"SportsAPING/v1.0/listEventTypes\", 
 \"params\": {\"filter\":{}}}
 ]"]

I have a question about creating this json string.

Simplified I would like to be able to create this string from something like

 {"jsonrpc" -> "2.0", "method" -> method, "params" -> {"filter" -> "{}"}}

where method contains for example

 "SportsAPING/v1.0/listEventTypes\".

such that the URLFetch still works.

Is this possible?

(I tried several ToString's but I don't get it to work except if json contains exactly the value

 "[
 {\"jsonrpc\": \"2.0\", 
 \"method\": \"SportsAPING/v1.0/listEventTypes\", 
 \"params\": {\"filter\":{}}}
 ]" 

including escape characters.

A different URLFetch ( or 11.0 equivalent ) is acceptable.

$\endgroup$
9
  • 2
    $\begingroup$ Export to JSON format? If you use an association representation, use RawJSON or the fast JSON functions from the Developer` context. $\endgroup$
    – Szabolcs
    Commented Oct 18, 2016 at 18:24
  • $\begingroup$ That's new for me, will check this out, thanks. $\endgroup$ Commented Oct 18, 2016 at 18:37
  • $\begingroup$ The question is how I transform an association to a string including escape characters for quotes in the string. $\endgroup$ Commented Oct 18, 2016 at 18:54
  • 2
    $\begingroup$ Add "Compact" -> True, that should do, right? $\endgroup$
    – Kuba
    Commented Oct 18, 2016 at 19:36
  • 1
    $\begingroup$ @niloderoock feel free to self answer, p.s. Compact option is documented on RawJSON export format. $\endgroup$
    – Kuba
    Commented Oct 19, 2016 at 11:55

1 Answer 1

1
$\begingroup$

Thanks to comments of @Kuba I was able to answer this question myself.

Note that the essence of the solution is the usage of

ExportString[string, "JSON", "Compact"->True]

Giving the function:

fetchData[json_] := ImportString[
  URLFetch[
    URLBETFAIR,
    {"StatusCode", "Headers", "Content"},
    "Body" -> json,
    Method -> "POST",
    "Headers" -> {"X-Application" -> DEVKEY, 
      "X-Authentication" -> TOKEN}][[3]],
  "JSON"]

The following solution works:

fetchData[ExportString[{
      {
       "method" -> "SportsAPING/v1.0/listCompetitions",
       "jsonrpc" -> "2.0",
       "params" -> {"filter" -> {"eventTypeIds" -> 
            getEventTypes[][[All, 1, 2]]}}
       }
      }, "JSON", "Compact" -> True]][[1, 2, 2]][[All, 1]][[All, 2]]

This is the solution I currently use:

getCompetitions[sel_] := fetchData[ExportString[{
       {
        "method" -> "SportsAPING/v1.0/listCompetitions",
        "jsonrpc" -> "2.0",
        "params" -> {"filter" -> {"eventTypeIds" -> sel}}
        }
       }, "JSON", "Compact" -> True]][[1, 2, 2]][[All, 1]][[All, 2]]
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.