Nikas ┼Żalias Nikas ┼Żalias - 4 months ago 10
JSON Question

How to get values of JSON object to string using javascript

I have a JSON object. For example:

{
"id": 1,
"name": "Name",
"surname": "Surname",
"number": "111111111"
}


How to make a string from this JSON object using javascript, that looks like this.


Name Surname 111111111

Answer

You can use for...in statement to iterating values of object. In loop add every value to string.

var json = {
    "id": 1,
    "name": "Name",
    "surname": "Surname",
    "number": "111111111"
};

var str = "";
for (var key in json) {
    str += (key != "id") ? (json[key] + " ") : "";
}
console.log(str);