0

I have the following arrays:

A=[1,2,3]
B=[6,7,8]

I would like to compose them into JSON format, specifically, as an array of objects. I am trying to write a function that will accept multiple arrays (in the example above A and B) and a corresponding number of keys (in this case: :"arrayA", "arrayB"), and will output them as an array of objects below:

[{"arrayA":1, "arrayB":6},{"arrayA":2, "arrayB":7},{"arrayA":3, "arrayB":8}]
Mary_Smith
  • 1
  • 1
  • 4

1 Answers1

-1

As pointed out below my answer is incorrect because lists do not have names but you could try to combine it with this answer to set up something that aims at what you are trying to do

How to get a variable name as a string in Python?

names = [a.__name__ for a in arrays] 
objs = [] 
For arrs in zip(arrays):
    objs.append({"array_" + n: val for n, val in zip(names, arrs)})
Community
  • 1
  • 1
Vince W.
  • 3,561
  • 3
  • 31
  • 59