Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert json values in comma separated string using javascript

I have following JSON string :

{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}

I want location_id as

3,2

like image 360
netfreak30 Avatar asked Oct 14 '16 06:10

netfreak30


People also ask

How do I convert a JSON to a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How are values separated in JSON?

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.

Can you convert JSON to CSV?

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*!

How to convert comma separated string to array using JavaScript?

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.

How to join a JSON string with no comma 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.

How do I convert an object's values to a comma-separated string?

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.

How to convert a comma-separated string to an array in PHP?

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.


2 Answers

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.

like image 120
Chrillewoodz Avatar answered Oct 02 '22 01:10

Chrillewoodz


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);
like image 34
Nina Scholz Avatar answered Oct 02 '22 00:10

Nina Scholz