0

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

Cells are: id, name, duration, date, and relationid

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!!

12
  • 2
    JSON insists that property names be quoted; it's not JSON if they're not. Commented Jul 6, 2018 at 21:42
  • JSON isn't converting the values to strings, they must have been strings in the original properties.data. Commented Jul 6, 2018 at 21:43
  • Thanks for your answer @Pointy but i'm trying to use this resource: docs.dhtmlx.com/gantt/desktop__loading.html Check how their loading data works. Commented Jul 6, 2018 at 21:44
  • 1
    @K3ny1 If you look at their tasks.json example, it has quotes around the property names. Commented Jul 6, 2018 at 21:45
  • 1
    keys of a JSON are always strings, even if you don't put double quotes on them. So what's the problem? Commented Jul 6, 2018 at 21:47

1 Answer 1

2

You can test whether the values look like integers, and parse them.

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;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hello, will test it now, btw I think in the else if is missing a ) Thanks!
Yep, I do that all the time here, too used to IDE that automatically balances....
Hello Barmar seems the code works well, but i'm facing a little problem that is my code is running before the it gets the value from the database, and is returning me a missing value error.
This code should be in the callback function of the database query.
Thanks so much @Barmar, i'm so near to do it. I need to remove also the "" from the id,name,duration,date titles. Is possible like you did?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.