2

Here is the json data I send through postman for 'specification' field:

{
"specification": [
    {
      "type": [
          {
              "type": "Smartphone , Phablet , Notch Phone , Camera Phone , Selfie Phone",
              "shape": "Bar"
          }
      ],
      "basic": [
          {
              "os": "Smartphone , Phablet , Notch Phone , Camera Phone , Selfie Phone",
              "Sim": "Bar"
          }
      ]
    }
  ]
}

Then in controller the validation rule is:

'specification' => 'required|json'

The error message: "The specification must be a valid JSON string."

how can I send and check valid json in laravel controller?

2 Answers 2

4

From documentation:

json

The field under validation must be a valid JSON string.

Your data has been parsed from JSON by Laravel itself, and specification is an array already, not a JSON string. You can check that by changing your rule to required|array and you'll see that it passes.

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

1 Comment

@TapashDatta Perhaps you could share more information how exactly you're sending this request: headers, body etc.
1

The string you are posting here is not a valid json string. A valid json string starts and ends with { and }. So in your case it should be like this:

{
    "specification": [{
        "type": [{
            "type": "Smartphone , Phablet , Notch Phone , Camera Phone , Selfie Phone",
            "shape": "Bar"
        }],
        "basic": [{
            "os": "Smartphone , Phablet , Notch Phone , Camera Phone , Selfie Phone",
            "Sim": "Bar"
        }]
    }]
}

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.