I have a list of tuples which are http headers. I want to convert the list to a JSON object. I try mochijson2 but to no avail.
So I have the following :
[{'Accept',"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
{'Accept-Encoding',"gzip,deflate"},
{'Accept-Language',"en-us,en;q=0.5"},
{'Cache-Control',"max-age=0"},
{'Connection',"close"},
{'Cookie',"uid=CsDbk0y1bKEzLAOzAwZUAg=="},
{'User-Agent',"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10"}]
And would like this ( a binary JSON string ) :
<<"{\"Accept\":\"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\",
\"Accept-Charset\":\"ISO-8859-1,utf-8;q=0.7,*;q=0.7\",
\"Accept-Encoding\":\"gzip,deflate\",
\"Accept-Language\":\"en-us,en;q=0.5\",
\"Cache-Control\":\"max-age=0\",
\"Connection\":\"close\",
\"Cookie\":\"uid=CsDbk0y1bKEzLAOzAwZUAg==\",
\"User-Agent\":\"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10\"}">>
And I try this where A is the original list of tuples :
list_to_binary(mochijson2:encode(A)).
I suspect I need to get it into a format that mochijson2 can interpret better. And then convert to binary. Or figure out a way to have all the characters represented as strings (rather than have some as list of integers).
Greatly appreciated if you could point me in the right direction with some sample code.
To convert Python tuple to JSON, use the json.dumps () method. The json.dumps () method accepts the tuple to convert an object into a json string. To work with json related functions, import json module at the head of the Python file. import json tup = ( "Dhirubhai", "Ratan", "Timothee" ) jsonObj = json.dumps (tup) print ( jsonObj)
In Erlang lists are strings basically, so if we want to convert list of values we need to specify that (tell ejson that it is not a string). - json ( { square, { number, side }}). - json ( { shapes, { list, data }}). to_json ( { shapes, [ { square, 4 }, { square, 6 }]}).
JSON library for Erlang on top of jsx. It gives a declarative interface for jsx by which we need to specify conversion rules and ejson will convert tuples according to the rules. I made this library to make easy not just the encoding but rather the decoding of JSONs to Erlang records.
This method is to convert a list to a tuple. list − This is the list which needs to be converted to a tuple. Returns a tuple based on the list provided. -module(helloworld). -export( [start/0]). start() -> io:fwrite("~w", [list_to_tuple( [1,2,3])]).
You need to convert those strings inside there into binary before you send it to the encoder. The mochijson2 encoder just considers this as a list of integers and outputs it as an array. So mochijson2 needs you to convert{'key', "val"}
into {'key', <<"val">>}
Try this in your code:
Original = [
{'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
{'Accept-Encoding',"gzip,deflate"}
].
StingConverted = [ {X,list_to_binary(Y)} || {X,Y} <- Original ].
Output = mochijson2:encode(StingConverted).
io:format("This is correct: ~s~n", [Output]).
Or if you prefer using Funs:
Original = [
{'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
{'Accept-Encoding',"gzip,deflate"}
].
ConvertFun = fun({X,Y}) -> {X,list_to_binary(Y)} end.
StingConverted = lists:map(ConvertFun, Original).
Output = mochijson2:encode(StingConverted).
io:format("This is correct: ~s~n", [Output]).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With