views:

59

answers:

2

Sorry if this is repost but I couldn't find my question after I posted it. I have a rails app that is rendering a json string and storing the same sting in the db. The output from the initial page that receives the output and the page that displays the data from the db have different output. The data and the function to format the data are identical. I'm stumped.

function:

function prettyPrint(jsonStr) {
   var jsonObj = jQuery.parseJSON(jsonStr);
   return '<pre>' + JSON.stringify(jsonObj,null,'\t') + '</pre>';
}

data:

{"Account":{"account_id":1},"response_details":[],"return_code":200,"Devices":[{"imei":"1234","name":"Device 1"},{"imei":"54321","name":"device 3"},{"imei":"354476024650842","name":"device 4 [no data]"},{"imei":"55124","name":"BlackBerry (8800)"},{"imei":"1234567890","name":"Garmin Sample"},{"imei":"987654321","name":"Second Garmin"},{"imei":"546787545678","name":"Tower 1"}]}

output 1: (from ajax)

{
    "Account": {
        "account_id": 1
    },
    "response_details": [],
    "return_code": 200,
    "Devices": [
        {
            "imei": "1234",
            "name": "Device 1"
        },
        {
            "imei": "54321",
            "name": "device 3"
        },
        {
            "imei": "354476024650842",
            "name": "device 4 [no data]"
        },
        {
            "imei": "55124",
            "name": "BlackBerry (8800)"
        },
        {
            "imei": "1234567890",
            "name": "Garmin Sample"
        },
        {
            "imei": "987654321",
            "name": "Second Garmin"
        },
        {
            "imei": "546787545678",
            "name": "Tower 1"
        }
    ]
}

output 2: (from db)

{
    "Account": {
        "account_id": 1
    },
    "response_details": "[]",
    "return_code": 200,
    "Devices": "[{\"imei\": \"1234\", \"name\": \"Device 1\"}, {\"imei\": \"54321\", \"name\": \"device 3\"}, {\"imei\": \"354476024650842\", \"name\": \"device 4 [no data]\"}, {\"imei\": \"55124\", \"name\": \"BlackBerry (8800)\"}, {\"imei\": \"1234567890\", \"name\": \"Garmin Sample\"}, {\"imei\": \"987654321\", \"name\": \"Second Garmin\"}, {\"imei\": \"546787545678\", \"name\": \"Tower 1\"}]"
}
A: 

Both the "Devices" array and the "response_details" arrays are being encoded as strings rather than arrays. Find where this is happening in the code that is putting the data in the database and fix that.

Joshua
Shouldn't jQuery.parseJSON(jsonStr) create a valid json object w/ arrays. There are no quotes around the arrays in the string passed.
Jeff Rossi
I would need to see your database save code to debug further. You should not need to parse the JSON before putting in the database, unless you are marshalling an object.
Joshua
A: 

I ended up formatting the output with ruby

  <% response = JSON.load(@order.response) %>
  <% main_counter = 1 %>
  <pre>
  {
  <% response.each do |k,v| -%>
     <% if v.is_a?(Hash) %>
        <%=k%>: {
           <% counter = 1 %>
           <% v.each do |k2,v2| -%>
           <%=k2%>: <%=v2.to_json%><% if counter < v.size %>,<%end%>
           <% counter += 1 %>
           <%end%>
        }<% if main_counter < response.size %>,<%end%>
     <% elsif v.is_a?(Array) %>
        <%=k%>: [
           <% counter = 1 %>
           <% v.each do |v2| -%>
           <%=v2.to_json%><% if counter < v.size %>,<%end%>
           <% counter += 1 %>
           <%end%>
        ]<% if main_counter < response.size %>,<%end%>
     <%else%>
        <%=k%>: <%=v%><% if main_counter < response.size %>,<%end%>
     <%end%><% main_counter += 1 %>
  <%end%>
  }
  </pre>
Jeff Rossi
yikes! Keep in mind you're probably covering up a bug somewhere else with this approach.
Joshua