0

"Hello everyone". I have a couple of days with these json's. I have a json String done, like this:

   String payload = JSON.serialize(
    new Map<String, Object> { 
        'jsonrpc' => '2.0',
        'method' => 'host.get',
        'params' => 
            new Map<String, Object> {
                'output' => new String[] { 'host' },
                'groupids' => new Integer[] { 1235 }
            },
        'auth' => result,
        'id' => 1
    }
);

And returns this:

{"id":1,"auth":"result","params":{"groupids":[1235],"output":["host"]},"method":"host.get","jsonrpc":"2.0"}

It is okay, but I'm Stuck with the following json format: (It should be the String returned)

{
    "issueUpdates" : [
        {
            "fields": { 
                "project": { 
                    "id": "10049"    
                }, 
                "issuetype": {   
                    "id": "10000" 
                },
                "customfield_10011": "EpicToGetBoard",
                "summary" : "DescripcionEjemplo"
            }
        }
    ]
}

The problem is with "[ ]". how to generate arrays [...]??

Any Idea guys?, Thanks

2
  • 1
    Hi and welcome the SFSE. I am not sure what the specific problem is since the first and second parts of the question seem unrelated. The latter requires an object with a property, issueUpdates, that is an array of objects with a property fields that contains various other properties. Where is the problem exactly? What have you tried? Where does it fail? Please edit the question to add some clarity.
    – Phil W
    Feb 28, 2022 at 7:32
  • Hi Phil, yeah, the json are not related, the first is not mine, the second one the final result that I should get with the string serialized. I edited the post already, the question exact was "how to generate arrays [...]"?. Thanks Feb 28, 2022 at 14:29

1 Answer 1

1

Assuming your question is how to generate arrays [...] using this coding pattern, the answer is to use new List<Object> for those e.g.:

String payload = JSON.serialize(
    new Map<String, Object> {
        "issueUpdates" => new List<Object> {
            new Map<String, Object> {
                "fields" => new Map<String, Object> {
                    ...
                }
            }
        }
    }
);
2
  • It works, thanks Keith! Feb 28, 2022 at 14:38
  • Personally I would drop the use of anonymous structuring like this and leverage Apex classes and the built-in type-based JSON serialization and deserialization capabilities.
    – Phil W
    Feb 28, 2022 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .