0

I'm using MySQL 5.6.43 and trying to search through in JSON array in a table. My JSON in MySQL database is like this:

[
    {
        "Name": "AAA",
        "CountryCode": "AFG",
        "District": "Kabol",
        "Population": 1780000
    },
    {
        "Name": "BBB",
        "CountryCode": "AFG",
        "District": "Qandahar",
        "Population": 237500
    },
    {
        "Name": "CCC",
        "CountryCode": "USD",
        "District": "Qandahar",
        "Population": 237500
    }
]

I want to take the just AFG CountryCode's. So my result should be like this:

[
    {
        "Name": "AAA",
        "CountryCode": "AFG",
        "District": "Kabol",
        "Population": 1780000
    },
    {
        "Name": "BBB",
        "CountryCode": "AFG",
        "District": "Qandahar",
        "Population": 237500
    }
]

How can I achieve that?

2
  • Parse the array of objects to separate objects (use JSON_TABLE() function), check each object's property, then aggregate matched objects back. Commented Oct 8, 2020 at 11:19
  • @Akina Thanks for your response. Can you explain it via code? Commented Oct 8, 2020 at 11:21

1 Answer 1

1
SELECT test.data, JSON_ARRAYAGG(JSON_OBJECT('Name',jsontable.Name,
                                            'CountryCode',jsontable.CountryCode,
                                            'District',jsontable.District,
                                            'Population',jsontable.Population)) filtered
FROM test,
     JSON_TABLE(test.data,
                '$[*]' COLUMNS (Name VARCHAR(255) PATH '$.Name',
                                CountryCode VARCHAR(255) PATH '$.CountryCode',
                                District VARCHAR(255) PATH '$.District',
                                Population VARCHAR(255) PATH '$.Population')) jsontable
WHERE jsontable.CountryCode = 'AFG'
GROUP BY test.data;

fiddle

In MySQL 5.6 you must use string functions - JSON is not implemented in this version yet:

SELECT CONCAT('[', GROUP_CONCAT('{', SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(test.data, '}', nums.num), '}', -1), '{', -1), '}'), ']') filtered
FROM test
CROSS JOIN (SELECT 1 num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) nums
WHERE LOCATE('"CountryCode":"AFG"', SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(test.data, ' ', ''), '}', nums.num), '}', -1))
GROUP BY test.data

fiddle

If source array may contain more than 4 objects per value then expand nums subquery.

Restriction: Neither key nor value to be searched must contain a space.

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

6 Comments

Is it valid for MySQL?
@sundowatch Have you seen the fiddle by link? it is executed in MySQL 8.0.21 successfully. But if the version is ancient then it will not work - JSON_TABLE was implemented in 8.0.4.
Hmm, ok. I'm using 5.6.43. Is there any equevelant for this?
@sundowatch For this version you must use common string functions.
Do you mean LIKE or any other common string functions?
|

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.