1

I need to have a JSON Formatted Base64 for this, but I am not sure how to change this string to have it as Base64 JSON Format.

public  String requestBody =  '{["Um05dkxFSmhjZz09"]}';

Um05dkxFSmhjZz09 this is already in Base64

  • Why do you need your JSON to be base64? – Adrian Larson Mar 1 '16 at 17:37
  • @AdrianLarson its for my test class Fake respond, and I receive String M = (String)JSON.deserializeStrict(responseBody,String.class); b = EncodingUtil.base64Decode(M); in my class, so my test string needs to be in that format – Metawaa Mar 1 '16 at 17:39
  • I am really confused with data structure inside string, it looks like a Hash Map without key but an array – karthikselva Mar 1 '16 at 17:41
  • @karthikselva its a fake response – Metawaa Mar 1 '16 at 17:42
  • do you want to create Base64 encoded value of 'Um05dkxFSmhjZz09'?? or the whole String '{["Um05dkxFSmhjZz09"]}'? – San Tosh Mar 1 '16 at 17:44
2

Encoding/Decoding is different from Serialization/Deserialization

  • The JSON you have for fake response is malformed.
  • If all you are getting from the response is Base64 encoded string, you don't need to deserialize
  • So in this case there is no need for JSON at all

Try this instead:

public String requestBody =  'Um05dkxFSmhjZz09';
b = EncodingUtil.base64Decode(requestBody);
| improve this answer | |
  • 1
    Thank you for the respond I just use this to make it JSON String formatJSON = JSON.serializePretty(requestBody); – Metawaa Mar 1 '16 at 17:55
1

A JSON works in a key-value pair.

When you are creating JSON here, I see the value, but not the key. Hence the solution would be to create a key-value pair instead.

Like:

public  String requestBody =  '{"yourkey": "Um05dkxFSmhjZz09"}'  //This is a JSON String

If you intend to use an array of values, use:

public  String requestBody =  '{"yourkeys": ["Um05dkxFSmhjZz09"]}'

and to validate any JSON in future, you could always use JSONLint

| improve this answer | |
  • 1
    Thankyou for the respond , I actually didnt know I could make it format it to json by String formatJSON = JSON.serializePretty(requestBody); it worked just fine – Metawaa Mar 1 '16 at 17:55
  • great! good to know :) – San Tosh Mar 1 '16 at 17:56
  • I posted the answer as well , I am not sure if its okay, but you gave a lot of good information thank you – Metawaa Mar 1 '16 at 18:01
  • 1
    its perfectly OK. you can mark your answer as best answer after 2 days... :) – San Tosh Mar 1 '16 at 18:20
1

in test cases we can actually use Base64 and do

String formatJSON = JSON.serializePretty(requestBody); 

to turn it into Json

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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