0

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"
      ]
    }
  }
5
  • Is that the json you are trying to manipulate, or that is the json result of your failed attempt? Commented Jul 13, 2018 at 16:50
  • @LawrenceCherone No still not binding the string. Commented Jul 13, 2018 at 16:58
  • @IncredibleHat that is the json i'm trying to manipulate. As you see the "text" array is empty. I updated the question as you can understand. Commented Jul 13, 2018 at 17:00
  • Yeah sorry add queryResult to it I missed that bit, answer is below. Commented Jul 13, 2018 at 17:05
  • Hehe... this is why proper indentation is always helpful :) I think you got your answer though, no? Don't forget to accept answers. Commented Jul 13, 2018 at 17:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@LawrenceCherone I edited the original, so now noone will know what we are talking about lol
0

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
                                        )

                                )

                        )

                )

        )

)

https://3v4l.org/SodvM

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.