mmdii
mmdii

Reputation: 184

save json as array in postgres db in django

i got problem that i can't handle. i have a JSON file that include some hotel data such rooms, name, city and so on. some of this fields include more than one data and separate with " | " like this one :

  "image_urls": "//imghtlak.mmtcdn.com/images/hotels/201512081111224987/exterior.jpg|//imghtlak.mmtcdn.com/images/hotels/201512081111224987/room.jpg|//imghtlak.mmtcdn.com/images/hotels/201512081111224987/download.jpg",

or like this one:

   "highlight_value": "1 lift|24 hour business centre|24 hour coffee shop|24 hour reception|24 hour room service|24 hour security|24 hours front desk|24-hour business center|24-Hour Front Desk|24-hr Coffee Shop|24/7 Power|Activities desk|Activity centre|Airport Transfer Available With Charges Rs.200/- Per Trip|Arrangements Of Kitty Parties & Birthday Party|Auditorium|Ballroom|Banqueting|Bar|Board Room|Boardroom|Breakfast buffet|Children's Playground|Doctor on Call|Dry Cleaning|Dry Cleaning Service|Dry cleaning/laundry service|Free garage parking|Free Parking|Free Wi-Fi|Free Wi-Fi Icon|Handicap Facilities|Hot tub|In-room safe (some rooms)|Laundry Service|Newspaper|Non-smoking rooms|Room Service|Room service (24 hours)|Room Service (24 Hours)(after 11:00pm.round The Clock Menu)|Room Service 24 Hrs|Room Service 6 Am - 12 Night|Room Service 7 Am To 9 Pm|Room Service 7am-11:30pm|Room Service, 24 Hour Reception|Room Service, 24 Hour Reception And Laundry Service|Room Services 7 Am To 9 Pm|Wake up call|Wheelchair access"

and i want to separate them to save in postgres db as array and i have django project that my frontend colleague told me it should be array that he can use them and show them and i'm new in database i have no idea how to do that i mean how to make them array in one filed so he can use them and sort them. sorry for bad english by the way this is JSON file : This is full JSON file

Upvotes: 0

Views: 159

Answers (1)

K. John
K. John

Reputation: 129

From what I understood, you can use something like this:

my_values = [
    {
        "highlight_value": "1 lift|24 hour business centre|24 hour coffee shop"
    }
]

new_items = []

for my_value in my_values:
    for values in my_value.values():
        if '|' in values:
            tokens = values.split('|')

            # REST OF CODE

Or, something like this:

elements = [
    values.split('|') 
        for my_value in my_values 
            for values in my_value.values() if '|' in values
]

Upvotes: 1

Related Questions