1

My API responds my call with the following JSON structure:

[
    {
        "order": {
            "total": "240.0",
            "completed_at": 1358432545000
        }
    },
    {
        "order": {
            "total": "720.0",
            "completed_at": 1359474090000
        }
    }
]

However, I want this JSON to be structured like this in order to use my data in Flot Graphs:

   {
        "label":"Order",
        "dataBar":[
            [
                1358432545000,
                240.0
            ],
            [
                1325635200000 ,
                720.0
            ]
        ]
    }

I've tried the following code, but it returns all data with comma separated, without '[' and ']'

var flotData = $.map(API_Response, function(i){ return [i.order.completed_at, parseInt(i.order.total)] });

How should I do that?

3
  • Can you post the code you've tried so far. Commented Jan 30, 2013 at 10:11
  • Are you modifying this json object usign JQuery? Create an object out of existing json, create a new empty object and add values to the new one. Then create JSON out of the new object. Commented Jan 30, 2013 at 10:12
  • 1
    I don't see why 3 users voted to close this as not a real question. It looks very real to me. Commented Jan 31, 2013 at 9:20

1 Answer 1

7

You can do this:

var data2 = {
    label: 'Order',
    dataBar: data.map(function(v){return [
         v.order.completed_at,
         parseFloat(v.order.total)
    ]})
};

Demonstration (open the console to see data2)

But please note there is nothing that can be called a "JSON structure" or a "JSON object".

What you have is a plain JavaScript object, even if it was sent to your browser encoded in JSON.


Now supposing you don't know the property of your object is called order, then you can do it with

var label;
for (var k in data[0]) {
   label = k[0].toUpperCase()+k.slice(1);
   break;
}
var data2 = {
    label: label,
    dataBar: data.map(function(v){return [
         v[k].completed_at,
         parseFloat(v[k].total)
    ]})
};

(It would be cleaner without support for IE 8, as I could have used Object.keys.)

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

5 Comments

Aren't completed_at and total properties of a deeper object?
@Boaz You're right. I fixed (and tested) the answer.
thank you very much for your answer. For general usage, is there any way to automate .order part, in case of json data changes ?
@CanCeylan See edit regarding automatic building of the label.
No, what I mean is, if the json has the following value: "SOMETHING_DIFFERENT_THAN_ORDER": { "total": "240.0", "completed_at": 1358432545000 }. So we won't be able to reach the values by writing v.order

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.