0

I'm trying to make a graph. S I need to send an ajax request, select some rows from database, then return the result. I did it. And here is the output:

success : function (data) {
    console.log(data);
}

enter image description here

To make that graph, I need to convert my current output to this structure: (this structure is the one I should pass it to the library which draws the graph)

  var json = [
    {
      "adjacencies": [
        {
          "nodeTo": "graphnode15", 
          "nodeFrom": "graphnode0", 
          "data": {}
        }, 
        {
          "nodeTo": "graphnode16", 
          "nodeFrom": "graphnode0", 
          "data": {}
        }, 
        {
          "nodeTo": "graphnode17", 
          "nodeFrom": "graphnode0", 
          "data": {}
        }
      ], 
      "data": {
        "$color": "#83548B", 
        "$type": "circle"
      }, 
      "id": "12", 
      "name": "sajad"
    }
  ];

I've tested all of these:

  • console.log(data);
  • console.log([data]);
  • console.log(JSON.stringify(data));
  • console.log("["+JSON.stringify(data)+"]");

But none of them isn't expected structure for the library which draws the graph. Anyway, Does anybody know how can I make expected structure?

8
  • @Kenny Doesn't work i.sstatic.net/6ZL8T.png Commented Jan 31, 2017 at 6:33
  • My bad its JSON in capital, not Json Commented Jan 31, 2017 at 6:34
  • @Kenny It throws this error: i.sstatic.net/HCfYT.png Commented Jan 31, 2017 at 6:35
  • What response you get from the ajax request? Commented Jan 31, 2017 at 6:36
  • @Kenny here is the response i.sstatic.net/Vsp7q.png Commented Jan 31, 2017 at 6:37

2 Answers 2

1

JSON.parse(data) will do this.

Try:

json =[]

json.push(data)

send this json to the graph

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

2 Comments

Not a Json, JSON.
Check your response has valid JSON format. If not, it throws exception.
1

Maybe this should work

success : function (data) {
    var json = [JSON.parse(data)];
    console.log(json);
}

1 Comment

Hmm, make sure the data return a raw JSON format.. try check it with alert(data);

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.