1

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

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

Supossing that you have this json stored in a var. for example:

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

You can do something like that:

var myStringResult = jsonObject.name + ' ' + jsonObject.surname + ' ' + jsonObject.number;

2 Comments

Yes that would be nice, but I need more universal approach, because I may get JSON object with different label names
You can use this function: JSON.stringify. Documentation here. developer.mozilla.org/es/docs/Web/JavaScript/Referencia/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.