List<List<String>> listaCot = new List<List<String>>();
    detalleCot = '{"Cdocu":"31","Ndocu":"","Codi":"'+variable+'"}';
    listaCot.add(new List<String>{detalleCot});
    jsonString = '{"Fecha":"'+fecha+'","Detalle":['+listaCot+']}';

//jsonString Shows = {"Fecha":"'+fecha+'","Detalle":[({"Cdocu":"131","Ndocu":"","Codi":"0301-011626"}), ({"Cdocu":"31","Ndocu":"","Codi":"0301-011627"})]}

The problem is that is adding () inside the json as you can see in the comments. Im trying to iterate and use join to remove the () but i cant put each element inside the jsonString variable.

        for (integer n = 0; n<listaCot.size(); n++){

//This shows each element without the ().

Is there a way to just remove the () or to put all the elements in another string ?


If you decided to use a plain string concatenation, you would need to use JSON.serialize:

jsonString = '{"Fecha":"'+fecha+'","Detalle":'+JSON.serialize(listaCot)+'}';

Note that you shouldn't be using these sorts of methods anyways; it's far more productive to use proper Objects and serialize them:

jsonString = JSON.serialize(new Map<String, Object> {
  'Fecha' => fecha,
  'Detalle' => listaCot
  • When doing the first option you said it shows this: ["{\"Cdocu\":\"31\",\"Ndocu\":\"\",\"Item\":2,\"Codi\":\"0301-011626\"] and the second shows something similar. – Oscar Aug 3 '19 at 23:30
  • @Oscar Yes, you should make the entire thing a set of nested maps/lists. Do not write plain JSON strings in your code. That's asking for trouble. – sfdcfox Aug 3 '19 at 23:48

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.