I have following JSON string :
{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}
I want location_id as
3,2
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
JSON has the following syntax. Objects are enclosed in braces ( {} ), their name-value pairs are separated by a comma ( , ), and the name and value in a pair are separated by a colon ( : ). Names in an object are strings, whereas values may be of any of the seven value types, including another object or an array.
Convert JSON to CSV format using Soda PDF's online converter tool. Turn JSON data into a CSV output. Here's how it works: upload JSON, convert to CSV & download. It's that simple & FREE*!
Convert comma separated string to array using JavaScript. Last Updated : 23 May, 2019. A comma-separated string can be converted to an array by 2 approaches: Method 1: Using split () method. The split () method is used to split a string on the basis of a separator.
If no separator argument is supplied, then the join () method will default to use a comma separator by default. 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: Show activity on this post.
To convert an object's values to a comma-separated string, use the Object.values () method to get an array of the object's values and concatenate them into a comma-separated string using the join () method, e.g. Object.values (obj).join (','). We used the Object.values method to get an array of the object's values.
A comma-separated string can be converted to an array by 2 approaches: The split () method is used to split a string on the basis of a separator. This separator could be defined as a comma to separate the string whenever a comma is encountered. This method returns an array of strings that are separated.
Simple:
var data = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
var result = data.map(function(val) {
return val.location_id;
}).join(',');
console.log(result)
I assume you wanted a string, hence the .join(',')
, if you want an array simply remove that part.
You could add brackets to the string, parse the string (JSON.parse
) and map (Array#map
) the property and the join (Array#join
) the result.
var string = '{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}',
array = JSON.parse('[' + string + ']'),
result = array.map(function (a) { return a.location_id; }).join();
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With