1

Team,

I'm trying to generate a JSON string but I keep running into issues with (I guess) quotes.

Code:

<?php 
// JSON data generated
$attachment = "[{
   'contentType': 'application/json',
   'content': {
      'type': 'mytype',
      'body': [{
         'type': 'TextInfo',
         'text': 'TEST TEXT',
         'wrap': true
      }]
   }
}]";
// Add attachment string (JSON data) in an array
$mydata = array(
  'space' => "abc",
  'markdown' => "**welcome**",
  'attachment' => $attachment
);
// Turn array into one big JSON string
$send_json = json_encode($data2);

What this should generate:

{
   "space": "abc",
   "markdown": "**welcome**",
   "attachment": [{
      'contentType': 'application/json',
      'content': {
         'type': 'mytype',
         'body': [{
            'type': 'TextInfo',
            'text': 'TEST TEXT',
            'wrap': true
         }]
      }
   }]
}

It feels like I'm missing something (besides the required knowledge ;-).

3
  • 2
    Yep, dont try and build JSON Strings manually. Create a class or array with the right contents and shape and then use json_encode() on it to generate the JSON String Commented Oct 8, 2019 at 16:46
  • Not sure why you want the sub objects to be in an array, as there is only one object in each case Commented Oct 8, 2019 at 17:11
  • Try typecasting $mydata = (object)array(); that should give you an object to work with in js rather than an array. Commented Oct 8, 2019 at 17:21

1 Answer 1

1

I don't understand how your $attachment data could be "JSON data generated" because it is invalid json ...I hope you aren't manually crafting that json string. You should always just generate an array/object and then call json_encode() when you are finished populating the iterable variable with data.

  • I have repaired your invalid $attachment string by replacing the " with ' and vice versa.

  • To nest this data inside $mydata['attachment'] you will need to decode the json first.

  • Finally, once your array data is completely built, THEN you call json_encode() on $mydata. The PRETTY_PRINT flag is just to aid readability in this post.

Code: (Demo)

$attachment = '[{
   "contentType": "application/json",
   "content": {
      "type": "mytype",
      "body": [{
         "type": "TextInfo",
         "text": "TEST TEXT",
         "wrap": true
      }]
   }
}]';

$mydata = array(
  'space' => "abc",
  'markdown' => "**welcome**",
  'attachment' => json_decode($attachment)
);

$send_json = json_encode($mydata, JSON_PRETTY_PRINT);
echo $send_json;

Output:

{
    "space": "abc",
    "markdown": "**welcome**",
    "attachment": [
        {
            "contentType": "application\/json",
            "content": {
                "type": "mytype",
                "body": [
                    {
                        "type": "TextInfo",
                        "text": "TEST TEXT",
                        "wrap": true
                    }
                ]
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

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.