Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  105] [ 3]  / answers: 1 / hits: 8238  / 4 Years ago, fri, january 24, 2020, 12:00:00

I have the following assessment which is to Create Array of Favorite Food items object in data.json file.



The Array of Objects should have the following fields :




  • Name

  • Type

  • Price



After writing the JSON data, this file should be imported in loopobject.js.



I tried the above request with the below data.json



{
 food:[
    {
      Name:Apple,
      Type:fruit,
      Price:100
    },
    {
      Name:pizza,
      Type:italian,
      Price:370
    },
    {
      Name:Burger,
      Type:Mac&Cheese,
      Price:310
    },
    {
      Name:salad,
      Type:Veg,
      Price:50
    }
  ]
}



And with loopobject.js



var json = require('./data.json');

json.forEach(function(object) { console.log(object.Name); });


and the Schema is:



const schema = Joi.array().min(3).has({
    Name: Joi.string().required(),
    Type: Joi.string().required(),
    Price: Joi.number().required(),
});



On submitting the above data.json and loopobject.js I get:



ValidationError: value must be an array



I am not sure how to edit the data.json to make it Array of Objects, as for as I can think the data.json has array of objects.
Can someone help fix the issue on data.json and loopobject.js?



Please note I can't edit the JSON schema


More From » arrays

 Answers
2

Your json is an object and has food named array. If you want to have just array, please change your JSON like this:



[
{
Name:Apple,
Type”:”fruit”,
Price:100
},
{
Name”:”pizza”,
Type”:”italian”,
Price”:370
},
{
Name”:”Burger,
Type”:”Mac&Cheese”,
Price”:310
},
{
Name”:”salad”,
Type”:”Veg”,
Price”:50
}
]

[#4955] Wednesday, January 22, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
Wed, May 15, 19, 00:00, 5 Years ago
;