Json stringify

Hello, related to the last question I have a doubt using JSON.stringify

I have a database, with multiple cells, and under each cell, values.

Cells are: id, name, duration, date, and relationid
In this database I have 3 rows:

I have this code:

  var result = {} 
  properties.data.forEach(addToResult); //Get data from database using properties.data

  instance.data.datavarb = JSON.stringify(result); //Send data after converted to JSON
  
  function addToResult(pair,isjson){ //operations
    if(isjson===true) result[pair.key] = JSON.parse(pair.value); else result[pair.key] = pair.value;
  }

I’m facing 2 problems:

1- First problem:
This is how i get the value after converted to JSON:
{“id”:“1”,“name”:“Football”,“duration”:“12”,“date”:“02-07-2018”,“relationid”:null}
How i need to be:
{id:1, name:“Football”, duration:12, date:“02-07-2018”, relationid:null}
Need remove the “” quotes from the numbers (id, duration and relationid) and the id,duration,relationid values.

2- Second problem:
In the problem 1, just for show you I only was parsing one of the three values from my database. What happens when I parse them all? This is how it looks like:

{“id”:“1, 2, 3”,“name”:“Football, France, Belgium”,“duration”:“12, 4, 3”,“date”:“02-07-2018, 08-07-2018, 10-07-2018”,“relationid”:“, 1, 1”}

Instead of creating one by one, it creates the same identifiers (id,name,duration) and put all the values in the same. For my prupose i need to be:

{id:1, name:“Football”, duration:12, date:“02-07-2018”, relationid:null},
{id:2, name:“France”, duration:4, date:“08-07-2018”, relationid:1},
{id:3, name:“Belgium”, duration:3, date:“10-07-2018”, relationid:1}

Many thanks!!

So.
#1: No, you don’t. the quotes around the keys are part of the notation, and Stringify will correctly handle numbers when it receives them. “1,2,3” is not a number, it’s a string of 5 characters.

  var a = {"athing":"quack","dohickey": 3};
  console.log(a.athing);

#2: Stringify will generate an array of objects.

var jdata = JSON.parse(JSON.stringify(yourdatabasearray));
console.log(jdata);
///Output
[{"id":1, "name":"Football", "duration":12, "date":"02-07-2018", "relationid":null},
{"id":2, "name":"France", "duration":4, "date":"08-07-2018", "relationid":1},
{"id":3, "name":"Belgium", "duration":3, "date":"10-07-2018", "relationid":1}]
///
console.log(jdata[0]);
///Output
{"id":1, "name":"Football", "duration":12, "date":"02-07-2018", "relationid":null}
///
console.log(jdata[0].name);
//Output
Football
///

So the quotes “” Should not give this error?

x3:8 Uncaught TypeError: Cannot read property ‘id’ of undefined at Object. (datastore_hooks.js:289) at o._parseInner (datastore.js:24) at o.parse (treedatastore.js:68) at Object.t._process_loading (load.js:87) at Object.t.on_load (load.js:80) at Object.t.parse (load.js:33) at HTMLDocument.eval (eval at g.create_code (x3:100), :53:7) at j (x3:8) at k (x3:8)

I’m trying to use: https://docs.dhtmlx.com/gantt/desktop__loading.html

Edit: What does this?

  var a = {"athing":"quack","dohickey": 3};
  console.log(a.athing);

I added it to jsfiddle, but only shows quack in console… so i don’t understand whats the point?

Thanks!

That means whatever you tried to give it is undefined. From the looks of that page you linked, it’s expecting you to give it an object with a data property that contains the above array.

The point of the bottom thing was to show you that the quotes dont stop you from referencing the property.

Thank you so much for helping @m_hutley as always.
Someone helped me to get this code:

var result = {} 
  properties.data.forEach(addToResult); //Get data from database using properties.data

  instance.data.datavarb = JSON.stringify(result); //Send data after converted to JSON

function addToResult(pair,isjson){ //operations
    if(isjson===true) {
        result[pair.key] = JSON.parse(pair.value); 
    } else if (/^\d+$/.test(pair.value)) {
        result[pair.key] = Number(pair.value);
    } else {
        result[pair.key] = pair.value;
    }
}

Which is cool because it removes the quotes from numbers. But still getting the quotes on the titles (id,name,duration,date,relationid) and i believe 99% the way i’m doing it i need to remove the quotes.
Need at least to test it and satisfy something inside me telling me remove remove remove remove remove.

So when you see this on the page you’re looking at:

{
  "data":[
     {"id":1, "text":"Project #1", "start_date":"01-04-2013", "duration":18},
     {"id":2, "text":"Task #1", "start_date":"02-04-2013","duration":8, "parent":1},
     {"id":3, "text":"Task #2", "start_date":"11-04-2013","duration":8, "parent":1}
  ],
  "links":[
     {"id":1, "source":1, "target":2, "type":"1"},
     {"id":2, "source":2, "target":3, "type":"0"}
  ]
}

or on this page https://docs.dhtmlx.com/gantt/desktop__supported_data_formats.html
… you’re 99% sure. Mkay.
You can try to remove the quotes. I have no idea how you would do that, because the quotes aren’t actually there, they’re a part of the object notation and not something that can be directly manipulated. But feel free to try.

The code you’ve shown sends forEach a function, but then expects the first argument of the function to be an object of the form ({"key":"somethinghere","value":"a value"}), and the second parameter to be a boolean. Array.forEach passes the current item as its first parameter, and the index as it’s second, so i’m not sure what that person was trying to do.

Inspect the properties.data by sticking console.log(properties.data); in just after the var result line.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.