Reputation: 10153
I have a problem with pattern match of json formatted string. Here I add a shorted version (just changed long json string to "{\"jsondata\"}" So i have this pattern match which is sucessfull:
> MyData2={ok,{{"HTTP/1.1",200,"OK"},
[{"connection","Keep-Alive"},
{"date","Thu, 10 Sep 2015 12:03:49 GMT"},
{"server","Apache/2.4.7 (Ubuntu)"},
{"vary","X-Auth-Token"},
{"content-length","1171"},
{"content-type","application/json"},
{"x-openstack-request-id",
"req-31b4efc1-2af4-4130-b7a8-01d94b456096"},
{"keep-alive","timeout=5, max=100"}],
"{\"jsondata\"}"}}.
After that I run the following:
> {ok,{{"HTTP/1.1",ReturnCode, State},B,J}}=MyData2.
unfortunatelly i get
If I change "{\"jsondata\"}"
to "jsondata"
the last pattern match works fine
I have no Idea how to extract the json and get in J the "{\"jsondata\"}"
I`ll appriciate any idea
** exception error: no match of right hand side value
Upvotes: 2
Views: 1698
Reputation: 1380
Your pattern matching operation works perfectly. I think the problem is, that one of the variables ReturnCode
, State
, B
or J
is already bound.
Lets assume the variable J
is already bound to a value, and the other variables are not. Depending on this value, the pattern matching operation
{ok,{{"HTTP/1.1",ReturnCode, State},B,J}} = MyData2.
either succeeds or not.
Case 1:J
is already bound to "{\"jsondata\"}"
Your pattern-match will succeed and the values of the unbound variables (ReturnCode
, State
and B
) will be set, according to the pattern of MyData2
.
Case 2:J
is already bound to "{jsondata}"
The J
-variable on the right hand side won't match the pattern of MyData2
on the left hand side. Thus the execution fails with an exception.
This also happens on the shell if you forget to clear your variables with f(Variable)
.
Upvotes: 3