Tules
Tules

Reputation: 4915

Storing nested JSON on the server

I have (mock) product data hard coded into a JSON file that I use to generate "catalogue" pages in my application. However, the ultimate plan is to allow users to add new products via a CMS style admin panel.

Obviously I need this data to be persistent. The obvious choice would be to use MySQL or some other database, but since this a a pretty complex data structure with lots of nesting I can't imagine how that could be achieved painlessly. I could simply store the JSON in a text file on the server but then I lose the ability to manipulate it and have to send the whole set each time I make a change. Then again that would only be when people added and removed products so perhaps it wouldn't be so bad. Perhaps I could decode the JSON with PHP, manipulated it and then re-serialize it into JSON and save it into a text file.

Sorry, I'm rambling here. What do you think are my best options?

{
        currency: currency,
        currencySymbol: CurrencySymbol(currency),
        shoes: {
            title: 'Shoes',
            items:[ 
                [
                    {
                        name: 'Light Shoes', 
                        price: 99.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Dark Shoes', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Brown Shoes',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    }
                ],[
                    {
                        name: 'Gold Shoes',
                        price: 999.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Dark Shoes', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    },{
                        name: 'Brown Shoes',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/shoes/darkshoes.jpg'
                    }
                ]
            ]
        },
        dance: {
            title: 'Dance Gear',
            items: [ 
                [
                    {
                        name: 'Cool Cap', 
                        price: 99.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Baggy Pants', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Jacket',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    }
                ],[
                    {
                        name: 'T-Shirt',
                        price: 99.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Hip-Hip Hoodie', 
                        price: 59.99, 
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    },{
                        name: 'Belt',
                        price: 5.99,
                        description: "Donec eu lacus vel sapien luctus fermentum. Nulla consequat viverra volutpat. Phasellus et sagittis mauris.", 
                        imgUrl:'./catalogue/dance/dance.jpg'
                    }
                ]
            ]

        }
    }

Upvotes: 0

Views: 58

Answers (1)

thpl
thpl

Reputation: 5910

Store json objects with a .json extension. No need for a text file here, and therefore no need to keep struggling with sending the correct headers.

For the persistence part: In your database table only reference the json files on the local file system along with other information you need (time it was updated etc.).

A sample table could look like this:

documents(id:pk, file, created_at, updated_at)

(Of course you could also store the raw json string in your table but you would run into danger using a data-type that is not suited to store the json and therefore cannot hold all of the data)

However this approach would only be suitable when you need to use relational database systems


However, a better approach would be to use document database systems such as mongoDB which also has been recommended in the comments. It's definitely worth a look, but it can be a bit awkward at first if you are coming from a relational dbms background.

Upvotes: 2

Related Questions