I processed a data in python and saved the file in JSON format.
However, I ran into a problem after getting the JSON file when I use them in node.js which is
the JSON file looks like as below.
{
"title": "Christmas Eggnog",
"ingredients": "['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']",
"id": "05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby"
},
The ingredients has to be a normal array however, it is an array held by quotation marks. And it eventually makes it impossible to read this json file as intended.
{
"title": "Christmas Eggnog",
"ingredients": ["whites", "yolks", "sugar", "rye", "whiskey", "brandy", "rum", "cream"],
"id": "05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby"
},
I would like to get the log value like below. When I read this JSON file in node.
const fs = require('fs');
let predata = fs.readFileSync('./data/testonce.json')
let data = JSON.parse(predata)
let newData = [];
data.forEach((d) => {
newData.push((d) => {
let targetarray = d.ingredients.split(',')
return {...d, ingredients: targetarray }
})
})
console.log(newData[0])
//"whites"
I can't get the string "white" because node doesn't understand the fact that
"['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']"
is an array.
Node sees it as one long string.
How do I turn this string into a normal array?
Edit)
My python code is as below.
import pandas as pd
df = pd.read_csv('./test.csv')
ingredients = df['ingredients']
for i in range(len(ingredients)):
ingredients[i]=str(ingredients[i])[1:-1]
ingredients
df['ingredients']=ingredients
df.to_csv ('./datatest.csv', index = False, header=True)
After this, I used csv->json formatter in this website. https://csvjson.com/[![enter image description here]1]1
Edit)
this is the csv file link
we.tl/t-Kw435qpNNd
Edit) Sample csv text This is the sample csv text.
title,ingredients,id Christmas Eggnog ,"['whites', 'yolks', 'sugar', 'rye', 'whiskey', 'brandy', 'rum', 'cream']",05zEpbSqcs9E0rcnCJWyZ9OgdH0MLby "Veal, Carrot and Chestnut Ragoût ","['chestnuts', 'veal', 'stew', 'meat', 'oil', 'onion', 'garlic', 'leaf', 'salt', 'chicken broth', 'wine', 'carrots', 'sage']",mF5SZmoqxF4WtIlhLRvzuKk.z6s7P2S Caramelized Bread Pudding with Chocolate and Cinnamon ,"['butter', 'brioche', 'quality', 'bread', 'crusts', 'eggs', 'egg', 'yolks', 'brown', 'sugar', 'cream', 'milk', 'vanilla', 'cinnamon', 'nutmeg', 'kosher salt', 'chocolate', 'top']",oQV5D7cVbCFwmrDs3pBUv2y.AG0WV26 Sherried Stilton and Green Peppercorn Spread ,"['stilton', 'cream cheese', 'peppercorns', 'brine', 'sherry']",Z9seBJWaB5NkSp4DQHDnCAUBTwov/1u Almond-Chocolate Macaroons ,"['almonds', 'sugar', 'cinnamon', 'salt', 'egg', 'almond', 'semisweet', 'chocolate']",bB3GxoAplVZeoX3fzWNWyeECtQFxw6G White Sauce or Bechamel Sauce ,"['butter', 'flour', 'milk', 'salt', 'pepper']",FHQAJvovVtPyKWlzgFEHgSUJsCM2Tjq