[Opa] Unserialize json

Frederic Ye frederic.ye at mlstate.com
Tue Jan 31 19:17:26 UTC 2012


Hello,

Here is a link that should help you : http://stackoverflow.com/questions/8776647/parsing-a-webclient-result-content-in-opa

On 31 janv. 2012, at 20:13, Clement Le Marc wrote:

> Hi,
> 
> I've been trying to unserialize some json but I haven't succeeded at the moment :(
> 
> I think it would be easier to copy/paste my code :
> 
> empty_args = {
>   lat = "1"
>   lng = "2"
>   alt = "3"
>   speed = "4"
>   device = "5"
>   os = "6"
>   locale = "7"
> }
> test_json_convert() = (
>   body = "\{\"lat\":\"37.78583526611328\",\"lng\":\"-122.40641784667969\",\"alt\":\"0\",\"speed\":\"-1\",\"device\":\"x86_64\",\"os\":\"iphone 5.0\",\"locale\":\"en\"\}"
>   do jlog("json={body}")
>   t = match Json.of_string(body) with
>     | {none} -> empty_args
>     | {some=json_raw} -> do jlog("json_raw={json_raw}")
>         match OpaSerialize.Json.unserialize(json_raw) with
>           | {none} -> empty_args
>           | {some=s} -> do jlog("s={s}")
>                         s
>   do jlog("t={t}")
>   content = (
>     <div>
>       <p>Nothing to see here, watch the logs...</p>
>     </div>
>   )
>   Resource.page("Test convert json",content)
> )
> Basically, I have a json string, I'm transforming it using Json.of_string() and then I'm trying to use the OpaSerialize.Json.unserialize() but I'm getting the following ouput :
> 
> json={"lat":"37.78583526611328","lng":"-122.40641784667969","alt":"0","speed":"-1","device":"x86_64","os":"iphone 5.0","locale":"en"}
> json_raw={Record = [{f1 = lat; f2 = {String = 37.78583526611328}}, {f1 = lng; f2 = {String = -122.40641784667969}}, {f1 = alt; f2 = {String = 0}}, {f1 = speed; f2 = {String = -1}}, {f1 = device; f2 = {String = x86_64}}, {f1 = os; f2 = {String = iphone 5.0}}, {f1 = locale; f2 = {String = en}}]}
> Finish unserialize "Fields ({alt : string; device : string; lat : string; lng : string; locale : string; os : string; speed : string}) are not found in type sum ([locale, os, device, speed, alt, lng, lat])"
> 
> I'm not really sure what the problem is here and there's not much documentation in opa source code so I may have missed something on how to convert json to opa :)
> 
> Thanks,
> Clément
> 
> 
> 
> _______________________________________________
> Opa mailing list
> Opa at lists.owasp.org
> https://lists.owasp.org/mailman/listinfo/opa

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.owasp.org/pipermail/opa/attachments/20120131/c8981611/attachment.html>


More information about the Opa mailing list