create a JSON document from two arrays

Im fairly newbie to this so apologies first off, I am endeavouring to create a JSON document, and there is a very strong possibility my methods are not the most common sensical to those in the know.

I have created two arrays, one containing the 'keys' as strings such as "a1", "a2" etc, and another to hold data.

Basically, i am reading data from my analogue pin which is connected to a mux chip. I read the data at the mux input, and store in the data array., i change the input, read and store again, and so on.

from here i want to use these two data arrays to create a JSON formatted document in the style of

{"a1":756, "a2":483,"a3":345.....} and so on.

I have hunted around the forum, and whether it be using the wrong words in a search, or other reasons, i cant find a straightforward answer.

My question is, am i making this difficult for my self, and if so, what is a more straightforward method, or if this is possible, how do i go about it.

Thanks in advance.

Tony

Do you need the ability to change the name of a value (a1, a2) or will they be fixed? If they are fixed then just have a single array for the values of the readings and create the JSON names on the fly.

The JSON data is being passed to Home assistant over MQTT. The 'key?' (a1 a2 etc, for the analogue readings) would remain static, with the data being amended each pass.
Would i create a new JSON every time 'on the fly' or amend the JSON statement each time.

i do feel i'm making this harder than it needs to be for a statement of 8 readings, but id like to understand for future reference.

You could use a for loop and strcat to build the JSON string bit by bit. Use itoa to convert each reading into a temporary string so you can strcat it. Be careful that the target string is large enough to accommodate all the pieces.

many thanks, i reckon i have enough of a way ahead.

Would probably be easier to use sprintf(), with the static text in the format string, although the command could get a bit lengthy depending on how many fields you are filling in. That also gives you the option of storing the fixed text in PROGMEM using sprintf_P().