1

I want to take all names from JSON delhi_hos file and store it in String, So that player can play That String. Right Now It plays one by one name. So Suggest Something Please.

JSON Example

[
 {
    "id": 1,
    "name": "JW Marriott Hotel",
    "country": "IN"
  },
  {
    "id": 2,
    "name": "Le Méridien Hotel",
    "country": "IN"
  },
  {
    "id": 3,
    "name": "The Leela Palace Hotel",
    "country": "IN"
  }
]

My Code

if "Hospital".lower() in sentence.lower():
# print("Which City?")
# print("1.Surat \n 2.Pune \n 3.Delhi")
        with open("store.txt", 'a') as store:
             store.truncate(0)
         if element['name'].lower() in sentence.lower():
              for items in delhi_hos:
                  name3 = items.get('name')
                  print(name3)
                  my_text = "Near is " + name3
                  my_obj = gTTS(text=my_text, lang=language, slow=False)
                  my_obj.save("welcome.mp3")
                  os.system("mpg123.exe welcome.mp3")
                  with open("store.txt", "r+") as text_file:
                       text_file.truncate(0)

and I want Names From JSON file in string Like This

"JW Marriott Hotel Le Méridien Hotel The Leela Palace Hotel" 

and the Store it in the variable.

var = "JW Marriott Hotel Le Méridien Hotel The Leela Palace Hotel"

So, I can use var as a input for my player to play this string. My main Problem Is to convert all name into this string.

Lucifer
  • 156
  • 4
  • 15
  • It seems like this question could be simplified. It doesn't look like you have any trouble with the JSON part, so you're really just asking how to play a bunch of sound files at once. Is that right? – glibdud Dec 26 '18 at 13:22
  • If so, there are several similar questions you might want to look at: [1](https://stackoverflow.com/questions/49237667/play-multiple-sound-files-simultaneously-through-raspberry-pi-preferably-using?noredirect=1&lq=1) [2](https://stackoverflow.com/questions/34535510/playing-a-lot-of-sounds-at-once/34830968#34830968) [3](https://stackoverflow.com/questions/39298928/play-multiple-sounds-at-the-same-time-in-python/39644525#39644525) – glibdud Dec 26 '18 at 13:24
  • No Sir, Here in the code you can see that I am getting all names from JSON and stored in "name3" var. And I am Storing again it with some text in "my_text". Then convert and store this text into "Welcome.mp3" File. But here's the problem It takes one name from file and stores and convert it into mp3 and then it plays. It does this for all names. I want it to take all the names together and store and convert it and play it. I don't want multiple sound Files Just One is enough. – Lucifer Dec 26 '18 at 13:45
  • Sorry, I guess I don't understand what you're trying to do. Maybe editing some sample data and output into the question would help? – glibdud Dec 26 '18 at 13:50
  • I think Now everyone will be able to understand the Problem. – Lucifer Dec 27 '18 at 06:33

2 Answers2

1

I hope this solves your problem. Add proper indentation if any error comes.

 import functools
    obj = [
     {
        "id": 1,
        "name": "JW Marriott Hotel",
        "country": "IN"
      },
      {
        "id": 2,
        "name": "Le Méridien Hotel",
        "country": "IN"
      },
      {
        "id": 3,
        "name": "The Leela Palace Hotel",
        "country": "IN"
      }
    ]

    var = functools.reduce(lambda a, b : a + " " + b["name"], obj, "")
    print(var)
varatharajan
  • 229
  • 2
  • 15
1

I don't know if I understand your question right. But as far as I understand, you simply want to have the values of the namefield in your JSON stringifyed. I'm wondering why whats solution doesn't work as expected. The simplest solution that comes to my mind would be:

names = ""      
for item in delhi_hos:
    names += item['name']

I suggest to start with small bites of a problem, when things don't work out. First add a print, when this works, assign variables, then add if statements and so on! Good Look!

Marvin W.
  • 887
  • 1
  • 6
  • 10