0

I have a column in a mysql database called params, this contains json data. See sample below;

{
  "menu_text": 1,
  "menu-meta_description": "My Website",
  "enable_page_title": "0",
  "page_title_heading": "h2"
}

I only want to update the enable_page_title key to 1, for every record in the table. I need to leave all other json values intact.

How can I achieve this?

1 Answer 1

1

You can use JSON modification function JSON_SET():

select json_set(js, "$.enable_page_title", 1) new_js from t;

Demo on DB Fiddle:

with t as (
    select '{
        "menu_text": 1,
        "menu-meta_description": "My Website",
        "enable_page_title": "0",
        "page_title_heading": "h2"
    }' js
)
select json_pretty(json_set(js, "$.enable_page_title", 1)) new_js from t;

| new_js                                                                                                                |
| -------------------------------------------- |
| {
  "menu_text": 1,
  "enable_page_title": 1,
  "page_title_heading": "h2",
  "menu-meta_description": "My Website"
} |
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! What's the js part for?
@TheOrdinaryGeek: js is the name of the column that contains your json data. I added a demo to my answer for your reference.

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.