0

This is my Json object for products which is stored in database. This json object is also storing another json object with images.
1.question: Is this the correct way of storing data about products in database or should be images stored separately?
2.question: Is format of this json object correct, i'm especially interested in sub json "images"? 3.question: How should JSON object look like if sub Json "images" would have like some json index array, where index would be the position of image

{
  "id_product": "1000",
  "name": "Rippin Unions",
  "description": "Vel aut sed neque enim illum mollitia. Molestias aperiam qui dolorem qui est. Dolore cumque dolore formatedtempore ipsum. Veniam molestiae laudantium voluptates numquam.",
  "date_c": "2015-12-31 02:06:32",
  "date_u": null,
  "id_category": "46",
  "id_user": "123",
  "alias": "rippin-unions",
  "active": "1",
  "images": "[
              {thumbImage:"http://lorempixel.com/250/150/?27403",image:"http://lorempixel.com/1024/768/?28526",position:4},
              {thumbImage:"http://lorempixel.com/250/150/?25207",image:"http://lorempixel.com/1024/768/?73566",position:1},
              {thumbImage:"http://lorempixel.com/250/150/?26960",image:"http://lorempixel.com/1024/768/?72686",position:0},
              {thumbImage:"http://lorempixel.com/250/150/?25168",image:"http://lorempixel.com/1024/768/?22297",position:2},
              {thumbImage:"http://lorempixel.com/250/150/?94897",image:"http://lorempixel.com/1024/768/?94418",position:3}
             ]"
   }

I'm askin this because currently i have problems printing JSON data. All i can do is call products[0].images, but if i try to select first thumb picture i fail. i Have tried many variations and nothing is working

If you need any additional question, please let me know and i will provide.
Thanks in advance

4
  • JSON supports arrays, so why don't you store images as an actual array: "images": [{thumbImage: "..."}, ...]. Then you can access the first image with products[0].images[0]. Commented Jan 5, 2016 at 17:19
  • because i'm new with JS and i'm searching for examples and good practices Commented Jan 5, 2016 at 17:21
  • If you google "json example", the first result already answers your question. Being new is no excuse. Commented Jan 5, 2016 at 17:24
  • dude, belive me i have searched everything, but sometimes you either don't know what you are searching and if this is the right way, like you said that json array looks like this. So please don't down vote and let the good people of stackoverflow help me Commented Jan 5, 2016 at 17:26

2 Answers 2

1

I would recommend giving this article a read from tutsplus

Copy and past the following at jsonlint to validate,

 {
    "id_product": 1000,
    "name": "Rippin Unions",
    "description": "Vel aut sed neque enim illum mollitia. Molestias aperiam qui dolorem qui est. Dolore cumque dolore formatedtempore ipsum. Veniam molestiae laudantium voluptates numquam.",
    "date_c": "2015-12-31 02:06:32",
    "date_u": null,
    "id_category": 46,
    "id_user": 123,
    "alias": "rippin-unions",
    "active": 1,
    "images": [{
        "thumbImage": "http://lorempixel.com/250/150/?27403",
        "image": "http://lorempixel.com/1024/768/?28526",
        "position": 4
    }, {
        "thumbImage": "http://lorempixel.com/250/150/?25207",
        "image": "http://lorempixel.com/1024/768/?73566",
        "position": 1
    }, {
        "thumbImage": "http://lorempixel.com/250/150/?26960",
        "image": "http://lorempixel.com/1024/768/?72686",
        "position": 0
    }, {
        "thumbImage": "http://lorempixel.com/250/150/?25168",
        "image": "http://lorempixel.com/1024/768/?22297",
        "position": 2
    }, {
        "thumbImage": "http://lorempixel.com/250/150/?94897",
        "image": "http://lorempixel.com/1024/768/?94418",
        "position": 3
    }]
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your JSON seems to be invalid, when you want to test your json structure sites like this are very helpful.

edit: In your case a valid JSON would be:

{ "id_product": "1000", "name": "Rippin Unions", "description": "Vel aut sed neque enim illum mollitia. Molestias aperiam qui dolorem qui est. Dolore cumque dolore formatedtempore ipsum. Veniam molestiae laudantium voluptates numquam.", "date_c": "2015-12-31 02:06:32", "date_u": "null", "id_category": "46", "id_user": "123", "alias": "rippin-unions", "active": "1", "images": "[{'thumbImage': 'http://lorempixel.com/250/150/?27403\','image': 'http://lorempixel.com/1024/768/?28526\','position': '4'},{'thumbImage': 'http://lorempixel.com/250/150/?25207\','image':'http: //lorempixel.com/1024/768/?73566\','position':'1'}]" }

1 Comment

it looks like yes, but i'm searching how valid one should look like. thx for effort and link any way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.