I've been delving into the world of ChartJs and have encountered a bit of a roadblock. I'm looking to decipher how to convert JSON data into labels and data for a bar chart.
The JSON data consists of an array of product orders. With varying numbers of products, I need to sift through and compile a list of products for the ChartJS labels. Following that, I need to tally how many times each product appears in the JSON in order to determine the number of times each product was sold.
[
{
"order_id": 1,
"product_name": "apple"
},
{
"order_id": 2,
"product_name": "orange"
},
{
"order_id": 3,
"product_name": "orange"
},
{
"order_id": 4,
"product_name": "monster truck"
},
{
"order_id": 5,
"product_name": "spark plug"
},
{
"order_id": 6,
"product_name": "apple"
},
{
"order_id": 7,
"product_name": "can of peaches"
},
{
"order_id": 8,
"product_name": "monster truck"
},
{
"order_id": 9,
"product_name": "orange"
},
{
"order_id": 10,
"product_name": "orange"
}
]
I aim to generate two arrays for the product labels and order counts
Labels: ["apple", "orange", "monster truck", "spark plug", "can of peaches"]
Orders: [2, 4, 2, 1, 1]
Any guidance on the best way to accomplish this task?