How do I set the values of part of a JSON object?

Hi,
I would appreciate some guidance on how to do the following on the server side:

Assume the following:

  1. I have a server action API that receives the JSON object P1 as a $_POSTed parameter.

  2. P1 looks like this:
    “P1” : {
    “var1” = “foo” ,
    “var2” = “bar”,
    “arrayA” = [ {“size”: “small”, “color”: “red”}, {“size”: “medium”, “color”: “blue”} ],
    }

  3. Inside my server action API, I will generate another array (manually or as the result of a database query, it does not matter) which will be called arrayB. This array may look like this:

“arrayB”: [ {“qty”: 32, “price”: 128} , {“qty”: 3, “price”: 12.8} ]

First question: How do I insert arrayB into the JSON object P1 ?
I want to have the new object P1 (which could be renamed to P2 if necessary) look like this:

"P1" : {
    "var1" = "foo" ,
    "var2" = "bar", 
    "arrayA" = [ {"size": "small", "color": "red"}, {"size": "medium", "color": "blue"} ], 
    "arrayB": [ {"qty": 32, "price": 128} , {{"qty": 3, "price": 12.8} ] 
}

Second question: How do I change the value of something inside P1?
Say for example, that I want to change P1.arrayA[1].size from “medium” to “large”

Any help that can be provided will be greatly appreciated!
Many thanks in advance,

Alex

@aschoijett I just posted an extension that takes care of that:

thank you all