This will convert it into an array (which is the JSON representation you specified):

var array = myString.split(',');

If you need the string version:

var string = JSON.stringify(array);
Answer from joeltine on Stack Overflow
๐ŸŒ
JBoss.org
developer.jboss.org โ€บ thread โ€บ 262563
convert a comma separated string to json array| JBoss.org Content Archive (Read Only)
The return type for JSONARRAY is clob. > If i convert the datatype the datatype of the column to clob, my odata url gives the below error ยท Do you have a primary key on column_string or are using it in another transformation where you expect it be comparable/sortable? By default clobs are not comparable. Generally what you would do is convert from clob to string (assuming that your json values are under 4000 characters) - cast(JSONARRAY(...) as string)
Top answer
1 of 3
4

For such a trivial example, it's pretty easy to produce a poorly-formed JSON String of your content and let JSONObject patch it up.

In a single expression:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:$2,")))
// {"art":0,"comedy":0,"action":0,"crime":0,"animals":0}

If you really want to keep the 0.0 as Strings:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:\"$2\",")))
// {"art":"0.0","comedy":"0.0","action":"0.0","crime":"0.0","animals":"0.0"}

If you want to account for possible extraneous whitespaces:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+)\\s*?,\\s*?([^,]+)(,|$)", "$1:$2,")))

.. will work with inputs like "art, 0.0, comedy, 0.0, action, 0.0, crime, 0.0, animals, 0.0" and other cases.


Disclaimer: it's not crazy-sexy code but drop in a one-line comment and it could be reasonable so long as the data structure stays simplistic.

2 of 3
2

You should use Map instead of Array (List of key/value pair is Map, not Array).

1 way: Using JsonObject

String [] arrayStr=strVal.split(",");
JsonObjectBuilder builder = Json.createObjectBuilder()

String key = null;
for (String s: arrayStr){
    if(key == null) {
       key = s;
    } else {
       builder.add(key, s);
       key = null;
    }
}
JsonObject value = builder.build();

2 way: Using Map

String [] arrayStr=strVal.split(",");
Map<String,String> map = new HashMap<>();

String key = null;
for (String s: arrayStr){
    if(key == null) {
       key = s;
    } else {
       map.put(key, s);
       key = null;
    }
}

// convert Map to Json using any Json lib (Gson, Jackson and so on)
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-string-comma-separated-values-to-JSONPath-format
How to convert string comma-separated values to JSONPath format - Quora
Answer: [code]import java.util.*; import java.util.regex.Pattern; public class Test2 { static Pattern p = Pattern.compile(","); private static final String USAGE_PT = "(@.usage"; private static final String USAGE_PT_OR = "||@.usage"; private static final String subusage_PT = "(@...
๐ŸŒ
Kodejava
kodejava.org โ€บ how-do-i-convert-csv-into-json-string-using-json-java
How do I convert CSV into JSON string using JSON-Java? - Learn Java by Examples
July 13, 2023 - package org.kodejava.json; import org.json.CDL; import org.json.JSONArray; public class CsvToJson { public static void main(String[] args) { // Comma delimited text created using text blocks String countries = """ ISO, CODE, NAME\s CZE, CZ, CZECH REPUBLIC\s DNK, DK, DENMARK\s DJI, DJ, DJIBOUTI\s DMA, DM, DOMINICA\s ECU, EC, ECUADOR """; // Convert comma delimited text into JSONArray object. JSONArray jsonCountries = CDL.toJSONArray(countries); System.out.println(jsonCountries.toString(2)); // Using a separate header and values to create JSONArray // from a comma delimited text JSONArray header
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ fundamental โ€บ javascript-fundamental-exercise-4.php
JavaScript fundamental (ES6 Syntax): Convert a comma-separated values string to a 2D array of objects - w3resource
July 3, 2025 - Use String.prototype.split('\n') to create a string for each row, then Array.prototype.map() and String.prototype.split(delimiter) to separate the values in each row. Use Array.prototype.reduce() to create an object for each row's values, with ...
๐ŸŒ
CSVJSON
csvjson.com
CSVJSON - CSVJSON
CSV to JSON bug fix: strings containing quotes and commas were prematurely cut. Make the GitHub repository public again. Re-opened to community. ... Transpose: You can now transpose the csv data before conversion. Output object instead of array: By default an array of objects is output. You can now output an object or hash. The first column becomes the hash key. Any data pasted and converted ...
Top answer
1 of 3
3

This can be achieved via the join() method which is built into the Array type:

const object = {
"id" : "122223232244",
"title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ",
"data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]
}

/* Join elements of data array in object to a comma separated string */
const value = object.data.join();

console.log(value);

If no separator argument is supplied, then the join() method will default to use a comma separator by default.

Update

If the JSON was supplied in raw text via a string you can use the JSON.parse() method to extract an object from the JSON string value as a first step like so:

const json = `{"id" : "122223232244","title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ","data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]}`

/* Parse input JSON string */
const object = JSON.parse(json);

/* Join elements of data array in object to a comma separated string */
const value = object.data.join();

console.log(value);

2 of 3
3

Access object properties using dot notation (e.g. obj.data) and then on the array you can use join to convert to a string with a comma in between.

const obj = {
    "id" : "122223232244",
    "title" : "ื”ืชืจืขืช ืคื™ืงื•ื“ ื”ืขื•ืจืฃ",
    "data" : ["ืขื•ื˜ืฃ ืขื–ื” 218","ืขื•ื˜ืฃ ืขื–ื” 217"]
}

console.log(obj.data.join(', '))

๐ŸŒ
Microsoft Power Platform Community
powerusers.microsoft.com โ€บ t5 โ€บ Building-Flows โ€บ Parsing-a-string-to-go-into-JSON-array โ€บ td-p โ€บ 322302
Solved: Parsing a string to go into JSON array
February 15, 2022 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.
๐ŸŒ
mdigi.tools
mdigi.tools โ€บ csv-to-json
CSV to JSON Converter - mdigi.tools
Convert CSV to JSON array online in easy to use tool with auto updates. ... Enter your CSV (comma separated values) data in the left inputbox provided.
๐ŸŒ
Microsoft Power Platform Community
powerusers.microsoft.com โ€บ t5 โ€บ Building-Flows โ€บ Json-Array-Output-as-String-Comma-Between โ€บ td-p โ€บ 1533776
Json Array - Power Platform Community - Microsoft
April 6, 2022 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.