Home > Back-end >  I have problem in data return from webhooks not json data
I have problem in data return from webhooks not json data

Time:07-15

I got this data returned from webhooks, but it's not a JSON object and can't use it currently. What kind of object is this and how can I Serialize this with C# or PHP?

{{
  "data": "{\"event\":\"INBOX\",\"from\":\"966******\",\"to\":\"966******\",\"text\":\"https:\\/\\/fs.magicrepository.com\\/incoming\\/4ef5613331213ab08888fb7cf72f332c.jpg\",\"alias\":\"\",\"pushname\":\"\",\"profilepicture\":\"\"}"
}}

CodePudding user response:

Try this. Convert the data from webhook in json decoded form and then save it.

$decoded_data = json_decode($data, true);

CodePudding user response:

Assuming you want to deserialize the input:

  1. trim the excess curly braces
  2. Decode the result as JSON
  3. Decode the value of the data property as JSON.

INn C#, it would look like this:

using System.Text.Json;


/* assuming input is like:
 * {{ 
 *  "data": "{\"event\":\"INBOX\",\"from\":\"966******\",\"to\":\"966******\",\"text\":\"https:\\/\\/fs.magicrepository.com\\/incoming\\/4ef5613331213ab08888fb7cf72f332c.jpg\",\"alias\":\"\",\"pushname\":\"\",\"profilepicture\":\"\"}"
 * }}
 */
var unwrapped = input[1..(input.Length - 1)];
var outerDoc = JsonDocument.Parse(unwrapped).RootElement;
var innerDoc = JsonDocument.Parse(outerDoc.GetProperty("data").GetString()).RootElement;
  • Related