1

I am actually looking for a way to validate elements in json. I thought there is a way to list them out to strictly avoid accepting wrong elements. For instance instead of "gender": "male" as illustrated below, someone could send "sex": "male" and I am trying to avoid it.

I have a data field (column) called Profile

profile = {'name': 'Payne', 'gender': 'male', 'favourites': [{'drinks': 'soda'}, {'colour': 'blue'}, {'game': 'scrabble'}], 'dob': '1962'}

I am using a third party API to populate the database using HttpClient.

My response is returning JSON and I want to make some decisions with it and store it in the database but I need to validate it in conformity with what is expected strictly.

4
  • JSON is not a "type", simply a data-exchange format. You could store the JSON as a string, or decode it and store it as a more complex type (an array, an object, etc). Or use its content to populate something else. It's up to you, and the needs and design of your application. Commented Nov 30, 2021 at 6:26
  • 1
    There is a way, decode it and validate it as an object/array. Commented Nov 30, 2021 at 19:51
  • Please can you demonstrate with the example above? Commented Nov 30, 2021 at 21:21
  • What have you tried so far? Where are you stuck? If this is coming from an HTTP response, why not simply decode the data and iterate over the resulting array? Commented Oct 1, 2023 at 10:47

2 Answers 2

1

About validation:

If you know how to do it with arrays, you can still decode it and validate it as an array, then encode it again. Symfony has a validator service but I do not know exactly how to correctly use it in all cases.

The official Symfony documentation for the Validator Service and how to use it can be found in this link and it's anchor: https://symfony.com/doc/current/validation.html

https://symfony.com/doc/current/validation.html#using-the-validator-service


Some info about JSON in PHP:

The most typical use for json, even for updating and downloading content to and from a DataBase, is the use of json_encode() and json_decode(), which will basically help you make your arrays more "portable" in different use cases.

For example, in MySQL, which accepts the JSON type, you can insert arrays into a column by encoding them with JSON.

If you want to declare a JSON type variable in Symfony, you can do it as in this example:

/**
 * @ORM\Column(type="json", ...)
 */

private $yourColumn;

Both json_encode() and json_decode() are available since PHP v5.2.0

As an example of a DB, the JSON type was added to MySQL in version 5.7.8 (https://dev.mysql.com/doc/refman/5.7/en/json.html)


You should take a look at these links:

https://www.php.net/manual/es/function.json-encode.php

https://www.php.net/manual/es/function.json-decode.php

https://www.w3schools.com/js/js_json_php.asp

https://dev.mysql.com/doc/refman/8.0/en/

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

2 Comments

Thank you so much! I know quite well. I am actually looking for a way to validate elements in json. I thought there is away to list them out to strictly avoid accepting wrong elements. For instance instead of "gender": "male", someone could send "sex": "male"
If you know how to do it with arrays, you can still decode it and validate it as an array, then encode it again. Symfony has a validator service but I do not know exactly how to correctly use it in all cases. symfony.com/doc/current/…
1

here is a symfony 6.3 example:

    /**
     * @var string[]
     */
    #[Assert\Json(message: "You've entered an invalid Json.")]
    #[ORM\Column(type: Types::JSON)]
    private array $roles = [];

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.