2

I have an array (it might be an object too, I don't know what I'm talking about):

grid.columns[0].text
grid.columns[1].text
grid.columns[2].text

And so on. I want to convert it into JSON. I've tried to use JSON.stringify(grid.columns.text) but it didn't work: it gives null.

5
  • What do you want to convert to JSON? The grid object or grid.columns array? Commented Jun 19, 2012 at 8:18
  • You have an array (grid.columns) whose elements have .text properties, while you are trying to stringify a .text property on the array itself. It is not clear what you are trying to do, so I cannot suggest how to fix your code. Commented Jun 19, 2012 at 8:19
  • @JamesAllardice, grid column header names. the grid object is very big. the grid.columns array. Commented Jun 19, 2012 at 8:19
  • As James Allardice said, did you try to convert grid.columns? That is: JSON.stringify(grid.columns); Commented Jun 19, 2012 at 8:23
  • @Angel, as I look through console.log(grid.columns) I see that the grid.columns is very very huge. Commented Jun 19, 2012 at 8:28

2 Answers 2

3

Try with

JSON.stringify(grid.columns.map(function(item) {
    return item.text;
}));
// ["value of text 0", "value of text 1",...]

Alternatively

JSON.stringify(grid.columns.map(function(item) {
    return {text:item.text};
}));
// [{"text":"value of text 0"},{"text":"value of text 1"},..]
Sign up to request clarification or add additional context in comments.

Comments

1

Using JSON.stringify(grid.columns.text) isn't going to work based off your provided structure:

Try the following instead:

JSON.stringify(grid.columns);

This should produce something like:

[
  {"text": "value"},
  {"text": "value2"},
  {"text": "value3"},
  ...
]

Comments

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.