Is there any benefit to base64 encoding a JSON payload? - json

I'm working with effectively a JSON API, that requires the JSON payload to be base64 encoded.
I can't imagine a good reason for encoding JSON as base64, it should be already safely utf8 encoded as valid JSON, so am I missing something?
Perhaps it's used as a way to serialise / compare the JSON, but I'm assuming that canonicalisation / comparison of JSON surely is a solved problem?

Related

binarized jason which looks good on browsers

I'm looking into an HTTP interface that returns (essentially) a JSON object.
When I access the URL by chrome or firefox, the JSON data is shown with appropriate indents. However, when I download it with curl etc, the data is binary.
I think the browsers know this binary encoding method and show it in a pretty format. (If I save it as a file from the browsers, it is a text file with the indents.)
What do you think this binary encoding is?
(Unfortunately, I can not upload the binary data here...)
[SOLVED]
Browsers send requests with headers but curl doesn't send header by default. That is the reason why I get the different response by these methods. My API returns binarized (compressed) json when called without a header.
You should have a look in the header of the HTTP response message which contains the binary data. There should be values about encoding, content-type and compression.
With this values you can decode the binary data.

Freebase API return raw JSON

The URL https://www.googleapis.com/freebase/v1/topic/en/alps returns a JSON string that is nicely formatted for reading. How can I tell Freebase to return the raw JSON string without all the unnecessary characters?
You can make the API request with GZIP enabled which will get rid of ALL the unnecessary bytes.
Accept-Encoding: gzip
You can also use the filter parameter to only return the property values that your app needs.
I don't think you can, but it shouldn't make a difference to any JSON parser. The whitespace isn't significant.

AFNetworking received non-English character: how to convert?

I am getting JSON response from some web server, say the server returns:
"kən.grætju'leiʃən"
I use AFNetworking and JSONKit, but what I've received is:
"æm'biʃən"
Not sure if it's AFNetworking's problem or JSONKit's problem, but any way, how to I parse and convert the string so it looks the same as from server?
Thanks
The server may be returning characters encoded in a way that violates the official JSON spec. If those characters are encoded as escaped unicode IDs (like \U1234) then JSONKit and NSJSONSerialization should both handle them fine.
If you can't change the server, you can work around the issue by URL-decoding the string - see https://stackoverflow.com/a/10691541/1445366 for some code to handle it. But if your server isn't following the correct specs, you're likely to run into other issues.

Why does SJCL report "this is not JSON" when trying to decode this JSON snippet?

I'm using SJCL, and it works fine with small ASCII strings.
But when I try to decode this piece of JSON (the result of the encryption of an HTML page) I get a "this is not JSON!" error.
The JSON has been produced by SJCL, and while I did encode it and decode it using LZW and base64 I don't get this error for small strings with the same workflow.
I tracked the error message origin to the decode function. I assume the regexes are failing but I don't understand why as this seems to be a perfectly formed JSON string to me.
However, I can be wrong as if I do a JavaScript eval on it it fails on a syntax error. But if I dump it in a file Python parse it fine.
The json that is at your this piece of json link starts and ends with a double-quote character. Is that actually part of the contents of the json? If it is, I believe that is your problem. Otherwise, it looks like valid json to me.
Ok I made a double passed base64 encoding. One before encryption, and one after. It seems that removing the first pass make it work.

J2ME, how to encode and decode Json?

how to encode and decode Json in J2ME?
Encoding it you can easily do manually, but for encoding and decoding here is a tutorial to help you.
http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/