0

sample pic of mysql table

Names of these 3 columns were Date, TargetName, Amount. TargetName column has partial rows with json array values and remaining rows with strings.

I want to extract value[0] from json array. With condition like [case when (targetName is Json type) Then $.value[0] else targetName] How to apply this condition and get the expected result?

Expected outcome: TargetName

SSC Stenographer

UP Police Sub Inspector

UPRVUNL JE

AAI JE Technical

ACC Exam

1 Answer 1

1
SELECT CASE WHEN JSON_VALID(TargetName)
            THEN TargetName ->> "$[0].value"
            ELSE TargetName
            END AS TargetName ,
       ...
Sign up to request clarification or add additional context in comments.

4 Comments

COALESCE function is not working for mysql server. Is there any alternative function for this please?
@HarikrishnaThummalapelly Well, conservative solution provided.
This is almost sorted. But these target names are stored as strings like "[{'language': 'English', 'value': 'West Bengal Group D'}, {'language': 'Hindi', 'value': 'West Bengal Group D'}]" How to deal with this?
@HarikrishnaThummalapelly Provide a fiddle (or CREATE TABLE + INSERT INTO, 3-5 rows with and without JSON as formatted code) and desired output, not as screenshot, into your question. And show desired output for provided source data.

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.