0

I know this questions exists like 100 times, but I just can't transfer the solutions to my code, so I hope you can help me. This should be pretty easy but I just don't get it working.

This is just my code with other variable because of reasons:

My Code:

for (var key in array) {
}

The JSON I want:

[{
    key: "One",
    y: 5
}, {
    key: "Two",
    y: 2
},];

Pseudo JSON:

[{
    key: key,
    y: array[key].data
},{
    key: key,
    y: array[key].data;
},];
4
  • Is array an object or an array? Commented Aug 3, 2015 at 7:22
  • Array is a list or an object? Commented Aug 3, 2015 at 7:23
  • 1
    JSON.stringify() Commented Aug 3, 2015 at 7:24
  • 2
    Note that nothing about this is anything to do with JSON - it's just plain old arrays and objects. Commented Aug 3, 2015 at 7:29

4 Answers 4

2

You can try this solution:

var data = [];
for (var key in array) {
  data.push({
    key :  key,
    y : array[key].data
  });
}

console.log(data);

But, what about Pseudo JSON:?

DEMO - See console (chrome) for output

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

1 Comment

Thanks a lot that is perfectly what I want!
2

I don't understand what is 'array'. Is it an object or an array?

I think what you want might be this, if 'array' is an array:

var new_arr = [];

your_array.forEach( function(entry) {
  new_arr.push({key: entry, y: entry.data}); // you should change it according to your need. 
})

return JSON.stringify(new_arr);

Or if 'array' is just an object, you may need this:

var new_arr = [];

for (key in array) {
  new_arr.push({key: key, y: array[key].data}); // you should change it according to your need. 
}

return JSON.stringify(new_arr);

2 Comments

@Yoshi foolish me... don't use 'new', use another word.
@Yoshi yea... I've edited it. Thank you for point out my mistake.
0

JSON is just a syntax for expressing objects and arrays independently of a scripting language's syntax. Apparently you want to convert your array into another structure and have this expressed in JSON. The conversion to JSON is usually performed by the built-in function JSON.stringify.

Assuming your array isn't really an array (which has only numeric indices, usually without gaps), but more an object-like structure, I'd suggest the following code:

var data = []
for (var key in array)
{
    data.push({key: key, y: array[key].data});
}
var json = JSON.stringify(data);
//...

If array really was an array you shouldn't use a for-in-loop. Otherwise you should consider renaming it to avoid confusion.

Comments

0

you can use following line to create an array of json

var jsonArr = [];

then you can create json object from following line

var obj = new Object();

put data in json object as following

obj['id'] = 123;
obj['name'] = 'ABC';

then put json object in json array as

jsonArr.push(obj);

you want to add multiple objects in json array then simply create json object and add one by one using push method.

[{"id":"123","name":"ABC"}]

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.