0

I have to build a json out of an array and send it through a socket to a java application and open it there. I have somehting like

array = ["a","b","c"]

{
  "events":[
        {"id":array[0], "name":"bla1"},
        {"id":array[1], "name":"bla2"}
   ],
   "name": "bla"
}

I have try to use concatenation to no success. How can I do it?

2
  • Show us the code that sends the data through the socket, and the code that opens it on the other end. Commented Aug 20, 2018 at 17:26
  • Concatenation of what?? Also, have a look a at the built-in json module. Commented Aug 20, 2018 at 17:28

1 Answer 1

1

Try:

o ={
    "events":[{"id": item, "name": "blah%s" %(index + 1)}
              for index, item in enumerate(array)],
   "name": "bla"
}

print(o)
# {'events': [{'id': 'a', 'name': 'blah1'}, {'id': 'b', 'name': 'blah2'}, {'id': 'c', 'name': 'blah3'}], 'name': 'bla'}
Sign up to request clarification or add additional context in comments.

2 Comments

this did what I wanted. Thanks!
@andresviena, awesome! Can you please accept it as an answer so other people know and I get those sweet sweet stackoverflow points?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.