0

I have a mysql table with a JSON column with data looking like that :

"[
{\"messageId\":\"0A0000000123ABCD1\",\"status\":\"request\\/0\",\"messagePrice\":\"0.03330000\"},            
{\"messageId\":\"0A0000000123ABCD2\",\"status\":\"request\\/0\",\"messagePrice\":\"0.03330000\"}
]"

How can I query the database to get the row containing a specific messageId (messageIds are uniques) ?

I tried :

DB::table('my_table')
 ->whereJsonContains('my_json_column',[['messageId'=>'0A0000000123ABCD1']]);

But I get no results.

Edit : I can make it work with a simple where like :

DB::table('notification_history')
    ->where('nexmo_responses','like','%0A0000000123ABCD1%')
    ->get();

But it's not really a clean way to do it.

2
  • your data looks like it's been double-encoded when it was inserted. That's probably the real root of your issue. Commented Aug 12, 2020 at 13:44
  • @ADyson Thanks you, didn't think about that. I was saving an associative array with json_encode, I just remove the json_encode and it's working perfectly. If you post your comment as an answer, I will accept it. Commented Aug 12, 2020 at 13:54

1 Answer 1

1

Your data looks like it's been double-encoded when it was inserted. That's the root of your issue.

If you ensure the data is only encoded once, then it'll be readable correctly when you want to access it again.

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.