This looks like a job for $.map!

var data = {
    "category": [{
          "id": 28,
          "name": "Dogs"
      },
      {
          "id": 14,
          "name": "Cats"
      },
      {
          "id": 878,
          "name": "Sheep"
      }]
}

var cats = $.map(data.category, function(v){
    return v.name;
}).join(', ');
Answer from gen_Eric on Stack Overflow
🌐
JSONata
docs.jsonata.org › string-functions
String functions · JSONata
If separator is not specified, then it is assumed to be the empty string, i.e. no separator between the component strings. It is an error if separator is not a string. ... $split("too much, punctuation. hard; to read", /[ ,.;]+/, 3) ~> $join(', ') => "too, much, punctuation"
🌐
AskPython
askpython.com › home › what is json and how to merge two json strings?
What Is JSON and How To Merge Two Json Strings? - AskPython
March 16, 2023 - Taking the same strings, we have tried to merge them into a dictionary by parsing them as dictionaries first using the jsonloads() and then merging them using the **. After converting the strings into a dictionary, we merged them into a data frame.
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.join java code examples | Tabnine
public String join(String separator) throws JSONException { return jsonArray.join(separator);
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › org.json.jsonarray.join
JSONArray.Join(String) Method (Org.Json) | Microsoft Learn
Returns a new string by alternating this array's values with separator. [Android.Runtime.Register("join", "(Ljava/lang/String;)Ljava/lang/String;", "GetJoin_Ljava_lang_String_Handler")] public virtual string? Join(string?
🌐
KNIME Community
forum.knime.com › knime analytics platform
How to concatenate json or string outputs from two nodes - KNIME Analytics Platform - KNIME Community Forum
December 24, 2019 - Hi, I want to produce a single JSON file with two key-value pairs. I have two database views which I have used in ‘DB Query Reader’ nodes and then I am converting two outputs to two different Json using ‘Table to Json’…
Find elsewhere
🌐
Medium
medium.com › @osamakhann118 › string-concatenation-from-json-array-how-to-concatenate-string-from-json-array-b2074ae34927
String Concatenation from JSON Array | How to concatenate string from JSON array - Osama khan - Medium
December 16, 2022 - String Concatenation from JSON Array | How to concatenate string from JSON array If you want to convert a JSON objects array to a concatenated string follow the following process lets say we have a …
🌐
Stack Overflow
stackoverflow.com › questions › 69761133 › join-all-json-object-string-values
mysql - Join all JSON object string values - Stack Overflow
select group_concat(json_unquote(json_extract(t.data, concat('$.', j.`key`))) separator '') as joined_string from mytable cross join json_table(json_keys(mytable.data), '$[*]' columns (`key` varchar(20) path '$')) j join mytable t on json_type(json_extract(t.data, concat('$.', j.`key`)))='STRING';
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2020 › 09 › 06 › javascript-merge-json-objects
JavaScript merge JSON objects – techtutorialsx
January 25, 2025 - Both JSON objects will be very simple, representing possible information about a person. One of them contains two properties: name and age. The other also contains two properties: an array of languages the person speaks and a Boolean indicating if the person is married or not. const string1 = `{ "name": "Todd", "age": 20 }`; const string2 = `{ "languages": ["Spanish", "Portuguese"], "married": true }`;
🌐
Newtonsoft
newtonsoft.com › json › help › html › MergeJson.htm
Merging JSON
JObject o1 = JObject.Parse(@"{ ... 'Roles': [ 'User', 'Admin' ] }"); o1.Merge(o2, new JsonMergeSettings { // union array values together to avoid duplicates MergeArrayHandling = MergeArrayHandling.Union }); string json = o1.ToString(); // { // "FirstName": "John", // ...
Top answer
1 of 3
1

It sounds like your @RemoteAction should return multiple addresses where each address looks like "city, postcode, citycode". For that to work I would expect that the JSON returned would be an array of the objects you post in the question not a single object.

The easiest way to parse such a structure is to submit an example of it to JSON2Apex and use the code that is generated.

The return type of your @RemoteAction method remains List<String> but each item in the list is:

calOut.add(p.city + ', ' + p.postcode + ', ' + p.citycode);

assuming p is a reference to the properties field of one of the returned and parsed objects.

PS

The names here depend on what the JSON fields are called but the code would look something like this:

List<String> calOut = new List<String>();
JSON2Apex parsed = JSON2Apex.parse(res.getBody());
for (JSON2Apex.Feature f : parsed.feature) {
    JSON2Apex.Properties p = f.properties;
    calOut.add(p.city + ', ' + p.postcode + ', ' + p.citycode);
}
return calOut;
2 of 3
0

Change the static method return type to string from the List<String> and

return the calOut list variable as

  @RemoteAction
  global static List<String> restapi(string accName)
  {

        string jsonStr;  
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://api-adresse.data.gouv.fr/search/?q=' + accName+ '');
        req.setHeader('Accept','application/JSON');
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        system.debug('res.getBody()===>'+res.getBody().replace('\n', ''));
        List<String> calOut = new List<String>();

        List<String> finalcalOut = new List<String>();

        jsonStr= res.getBody();

        JSONParser parser = JSON.createParser(jsonStr);

        while (parser.nextToken() != null) 
        { 
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) 
            {

               String fieldName = parser.getText();

               if(fieldName == 'label')
               {
                  parser.nextToken();
                  calOut.add(parser.getText());               
               }        

               if(fieldName == 'context')
               {
                    parser.nextToken();
                    calOut.add(parser.getText());               
               }
               if(fieldName == 'city')
               {
                  system.debug('city  ---->'+parser.getText());  
                  parser.nextToken();
                  calOut.add(parser.getText());               
               }    
             }
             finalcalOut.add( String.join(calOut,', '); );
         }   
         return finalcalOut;
      }
    }

You would be getting something like

51, Marren, Champagne-Ardenne, Sezanne, 51120

🌐
Jon Does Flow
jondoesflow.com › post › retrieve-values-as-a-concatenated-string-from-json-object
Retrieve values as a concatenated string from JSON object
April 30, 2023 - In the screenshot below, I am selecting the Ouputs from the dynamic content from the compose step and then mapping the column "Value" to the value item from the JSON object. You'll need to write an expression for this to get this, but it will change and resolve to Value as shown in the screenshot below. That expression is item()?['Value'] After this, we need to set the StringValue variable to join each of the values from the Select statement into one string, this is done by using the following expression: join(body('Select'),',')
🌐
Medium
allaboutcode.medium.com › javascript-how-to-merge-2-json-strings-together-and-return-a-string-359455bc48f1
Javascript — How to merge 2 JSON strings together and return a string | by Marika Lam | Medium
June 14, 2022 - const string1 = `{"name": "Todd","age": 20}`;const string2 = `{"languages": ["Spanish", "Portuguese"],"married": true}`; 2. After defining these JSON strings, we will parse them to JavaScript objects.