0

I'm new to programming so bear with me XD I've spent 2 days trying to work this out to no avail.

I have a chart in javascript that gets data from test.json that is saved in the same directory as the chart's file :

data2020 = '[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975]';
data2021 = '[58578,15864]';

As I learned how to update the data, I realized it's not the standard json format as the updated file from this script :

file_put_contents('test.json', $newJsonString);

is

[{"data2020":[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975],"data2021":[58578,15864]}]

and now my chart cant read that json file. I figured what's wrong must be in my chart since I only use this :

  data: {
    labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
    datasets: [{
      label: "Produksi 2020 : ",
      lineTension: 0.3,
      backgroundColor: "rgba(78, 115, 223, 0.05)",
      borderColor: "rgba(235, 180, 39, 1)",
      pointRadius: 3,
      pointBackgroundColor: "rgba(235, 180, 39, 1)",
      pointBorderColor: "rgba(235, 180, 39, 1)",
      pointHoverRadius: 3,
      pointHoverBackgroundColor: "rgba(255, 184, 0, 1)",
      pointHoverBorderColor: "rgba(255, 184, 0, 1)",
      pointHitRadius: 10,
      pointBorderWidth: 2,
      data: JSON.parse(data2020),
    },

then I add both script to my php with this (as someone suggested in the other post below) :

<script src="../js/test.json"></script>
<script src="../js/chart.js"></script>

Notice how the only thing I did was use JSON.parse and nothing else. I figured I had to find a way to put my json file to an array, and use that array in the data. But I cant figure out how to do that. So the question is really simple, how do I convert json file to an array in javascript?

I've read this link, but I still dont understand it XD : How to read an external local JSON file in JavaScript?

RyGnwn
  • 11
  • 1
  • Welcome to SO, I would recommend reading [mcve] as that will assist you in asking the best question. Particularly important is your code that is used to load the json file. – imvain2 Feb 09 '21 at 22:15
  • use `JSON.stringify` to convert object to string and `JSON.parse` to convert to object. Assuming `$newJsonString` is the stringified thing `[{"data2020":[0,0,0,0,0,202...rest_of_info`, `let data2020=JSON.parse($newJsonString).2020` for example – The Bomb Squad Feb 09 '21 at 22:19
  • Please post entire code - otherwise we're just reading between the lines and guessing. – see sharper Feb 09 '21 at 22:21

1 Answers1

-1

JSON files usually start and end with and { and }, so if you add those, JSON.parse() should be able to read the file. For example, change

[{"data2020":[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975],"data2021":[58578,15864]}]

to

{[{"data2020":[0,0,0,0,0,202,50857,116823,113280,109400,99846,83975],"data2021":[58578,15864]}]}
  • 1
    JSON files do not have to start and with braces. The format the OP had is perfectly valid. Stick it into an online parser and see. – see sharper Feb 09 '21 at 22:16
  • Then my approach should be, how to change"file_put_contents('test.json', $newJsonString);" php script to something else? – RyGnwn Feb 09 '21 at 22:20