Форматирование Json, неожиданное ‘:’

#javascript #json

Вопрос:

У меня есть следующий файл JSON:

 {
    "breakfast": {
        "lose": [
            {
                "name" : "Fruit Shake",
                "ingredient" : "Up to 300g of fresh fruit",
                "recipe": "Put all the chosen fruit in the mixer, unitl you have a liquid fruit shake, that as low calories and a good taste",
                "macro-review": "Some carbs, represented by natural sugar, a lot of vitamin"
            },
            {
                "name" : "Avocado and smoked salmon toasts",
                "ingredient": "Avocado, toast, smocked salmon", 
                "recipe": "Put the avocado on the toast, and then the smocked salmon. If you really want to lose fat, don't put too much avocado",
                "macro-review":"A few carbs, represented by bread, and a lot of protein, represented by avocado and salmon"
            }
        ],
        "main": [
            {
                "name" : "Banana milk",
                "ingredient" : "Milk, banana, cereals",
                "recipe": "Cut the banana in small slice, and then put it with cereals in the milk",
                "macro-review":"Carbs represented by cereals and banana, and the good fat and the protein of the milk. Totaly healthy"
            },
            {
                "name" : "Proteic pancake",
                "ingredient": "Greek jougurt, oats flour, eggs", 
                "recipe":"After mounting the albums to snow, add the flour oats and yogurt mixing. cook in one non-stick pan",
                "macro-review":"High in protein, only a few of carbs"
            }
        ],
        "gain": [
            {
                "name" : "Greek jougurt with nuts",
                "ingredient":"Greek jougurt and nuts, but also every type of dried fruit. If you want, add also some berries",
                "recipe": "Put the nuts in the jougurt, and shake a bit",
                "macro-review": "Preatty rich in carbs, due of the nuts, but also in protein"
            },
            {
                "name": "Coconut coffee shake",
                "ingredient": "Caffe, dried coconut, sugar, milk", 
                "recipe": "Put the coffe and the milk in the same glass, and shake. Then add the sugar, the coconut. Add also other dried fruit if you want",
                "macro-review": "High in carbs, but also with an high protein due of the presence of the milk. "
            }
        ]
    },
    "lunch": {
        "lose": [
            {
                "name" : "Zucchini spaghetti with tomato",
                "ingredient" : "Zucchini and tomato sauce",
                "recipe": "Create the spaghetti using a spiralizator, then put the tomato sauce, and let it cooks for some minute",
                "macro-review": "Rich in vitamin, rich in fiber, no carbs, only a few of protein"
            },
            {
                "name": "Mediterranean salad",
                "ingredient": "Boiled eggs, lettuce, tomato, olives",
                "recipe": "Boil the egg, cut lettuce and tomato, then put all the ingredients in the same recipe",
                "macro-review": "Good amount of protein, due of the egg, no carbs, good amount of vitamin and fiber"
            }
        ],
        "main": [
            {
                "name" : "Pasta genovese",
                "ingredient" : "Pasta (100g per person), pesto, potatos, basil",
                "recipe": "Cook the pasta normally, cut in very small pieces the potatos, and then put them with the pesto. When the pasta is coocked, put everithing toghether",
                "macro-review": "Good carbs amount, high in fiber, has also deoxindants"
            },
            {
                "name" : "Chicken salad",
                "ingredient":"Chicken, lettuce, a choosen sauce, better if light",
                "recipe": "Cut the chicken in small pieces, asnd then cook it. Put the coocked chicken on lettuce leaf with the sauce. You can eat now, or make a wrap with another lettuce leaf",
                "macro-review": "No carbs, high protein, good amount of fiber"
            }
        ],
        "gain": [
            {
                "name": "Basmati rice with scrambled egg",
                "ingredient": "Basmati rice, and two eggs",
                "recipe":"Cook the rice and make scrambled eggs. Then destroy the egg, and put everything toghether. It would be delicius and healthy",
                "macro-review": "High in carbs and protein"
            },
            {
                "name": "Potatoes and chicken",
                "ingredient": "Potatoes and chiocken",
                "recipe": "Grill the chicken, and backes potatoes. Choose the same amount of chicken and potatoes",
                "macro-review": "High both in carbs and in protein. Has also a few of good fat. Exelent for gain weight"
            }
        ]
    },
    "dinner": {
        "lose": [
            {
                "name":"Salmon salad",
                "ingredient":"Salmon, lettuce, tomatos",
                "recipe": "Cut tomato and lettuce as you prefer. You can choose if you want to cook salmon. Then, put everything toghether",
                "macro-review":"High in fiber, with a good amount of protein"
            },
            {
                "name":"Cuscus",
                "ingredient": "Cuscus, olives, carrot",
                "recipe":"Prepare the cuscus following steps in the container, cut the carrots. When everything is ready, melt everithing toghether",
                "macro-review":"High in carbs, but also in fiber and vitamin"
            }
        ],
        "main": [
            {
                "name":"Fish spaghetti",
                "ingredient":"Spaghetti, shrimps",
                "recipe": "Cook the spaghetti and the shrimps in different pots, then melt everithing. If you want, add basil or some spicies",
                "macro-review": "Good amount of carbs and protein, with also fiber"
            },
            {
                "name": "Rich sandwick",
                "ingredient":"Bread, lettuce, tomatoes, chicken meat stripes",
                "recipe":"Cook the chicken meat stripes. Then put all the ingredients in the bread. Enjoy",
                "macro-review": "Low carbs, good in protein, fiber and vitamin"
            }
        ],
        "gain": [
            {
                "name":"Friulan frico",
                "ingredient": "Unripened cheese, potatoes",
                "recipe": "Cut in small peaces the potatoes, then melt the cheese in the same pan of the potatoes, until it grills a bit",
                "macro-review":"Good amount of fat and protein"
            },
            {
                "name": "Pork potatoes",
                "ingredient": "Pork chop, potatoes, carrots and barbecue sauce",
                "recipe": "Cook the pork chop. Cut and cook both potatoes and carrots. Put everything toghether, and add the barbecue sauce",
                "macro-review": "High protein, carbs, and carotene"
            }
        ]
    }, 
    "snack" : {
        "lose": [
            {
                "name" : "Berries jougurt",
                "ingredient": "Greek jougurt and random berries",
                "recipe": "Put the berries in the same cup with the greek jougurt. That is all",
                "macro-review": "Good amount of protein and vitamin"
            },
            {
                "name" : "Fruit salad",
                "ingredient": "Random fruit, not dried",
                "recipe": "After choosing the fruit, cut it and put everithing in a bowl. If you want, add some lemon juice",
                "macro-review": "Carbs, represented by good frui sugar. High in vitamin and fiber"
            }
        ],
        "main": [
            {
                "name":"Salmon burger small",
                "ingredient": "A small bread, salmon",
                "recipe": "Put the salmon beetwen the two half of the bread. Don't choose an enormus bread",
                "macro-review": "Both carbs and protein"
            },
            {
                "name": "Banana and strawberries shake",
                "ingredient":"Milk, banana, strawberries",
                "recipe":"Put everything in the mixer, until is a liquid uniform delicius meal",
                "macro-review": "High in vitaimn, carbs, and with a few of good fat"
            }
        ],
        "gain": [
            {
                "name":"Avocato toast",
                "ingredient": "Avocado, toast",
                "recipe": "Put the content of the avocato on the toast bread",
                "macro-review": "High in protein, with some carbs"
            },
            {
                "name":"Sweet jougurt with nuts",
                "ingredient":"Honey, white jougurt, nuts or dried fruit",
                "recipe":"Put the honey in the jougurt. Mix it, and then add nuts",
                "macro-review": "High in carbs, protein, and with a few of good fat"
            }
        ]
    }
}
 

Я пытаюсь импортировать его в свой файл javascript. Но в консоли есть эта ошибка:

 Uncaught SyntaxError: Unexpected token ':'
 

Я попытался использовать онлайн-валидатор json, но ничего не изменилось. Также vscode не показывает ошибку в формате. Кто-нибудь знает, почему? Если я не смогу решить эту проблему, я не смогу двигаться вперед. Спасибо

Комментарии:

1. Пожалуйста, покажите нам свой код.

2. trying to import it in my javascript file — Как?

3. Возможно, вы заключали его в «s» вместо «s»?

4. @Poss если это должен быть ответ на вопрос, пожалуйста, опубликуйте его как таковой. Или вы используете две учетные записи, и это должно быть разъяснением вашего вопроса? Затем, пожалуйста, отредактируйте его, чтобы он содержал всю соответствующую информацию

5. Пример является допустимым JSON, но он не является допустимым JS. Назначьте строку переменной.

Ответ №1:

Допустим, у вас есть вышеупомянутый JSON в data.json файле. Внутри вашего файла js

 const data = require('<relative_path_to_data.json_file>');

// your code

 

Примечания

  • Вы можете использовать импорт вместо require , если вы используете ES6
  • Убедитесь, что вы используете относительный путь в требовании/импорте

Комментарии:

1. Пожалуйста, поделитесь более подробной информацией. Как это устраняет данную ошибку? Имейте в виду, что хорошее объяснение помогает другим извлечь уроки из вашего ответа