Reputation: 27
I want to set a string value to the text
array inside the text
object which is inside the fulfillmentMessages
JSON array using PHP.
I am currently trying with this code, but have failed.
$response->fulfillmentMessages[text]->text = "Hello ";
echo json_encode($response);
The JSON I am getting with this:
{
"responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
"queryResult": {
"queryText": "yes",
"action": "send.first.question",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"outputContexts": [
{
"name": "projects/quizbot-90a24/agent/sessions/9c08dfd274-7ece-19fe-c984-f5cd5ef135c7/contexts/expressname-followup",
"lifespanCount": 1,
"parameters": {
"userName": "saliya",
"userName.original": "saliya"
}
},
{
"name": "projects/quizbot-90a24/agent/sessions/9c08fdvfsd4-7ece-19fe-c984-f5cd5ef135c7/contexts/sessionusername",
"lifespanCount": 4,
"parameters": {
"userName": "saliya",
"userName.original": "saliya"
}
}
],
"intent": {
"name": "projects/quizbot-90agd24/agent/intents/063ed465-0ca1-4d94-bf7c-5b7ac4c31f6f",
"displayName": "expressName - yes"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"end_conversation": true
},
"languageCode": "en"
}
}
This is the result I intend to take:
"fulfillmentMessages": [
{
"text": {
"text": [
"Hello"
]
}
}
Upvotes: 0
Views: 980
Reputation: 46602
You're trying to manipulate an array item in nested object and array, so you need to change it to.
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
Example:
<?php
$response = json_decode('{
"responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
"queryResult": {
"queryText": "yes",
"action": "send.first.question",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [{
"text": {
"text": [
""
]
}
}]
}
}');
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
print_r($response);
Result:
stdClass Object
(
[responseId] => b19f0045-830e-4d12-a57e-07cdfd55f
[queryResult] => stdClass Object
(
[queryText] => yes
[action] => send.first.question
[parameters] => stdClass Object
(
)
[allRequiredParamsPresent] => 1
[fulfillmentMessages] => Array
(
[0] => stdClass Object
(
[text] => stdClass Object
(
[text] => Array
(
[0] => Hello
)
)
)
)
)
)
Upvotes: 0
Reputation: 1918
your indentation threw me for a second. this will work
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
I didnt notice that fulfillmentMessages
was a property of queryResult
.
Upvotes: 1