0

I have the following table def:

`CREATE TABLE `TestInfo` (
  `Info` json DEFAULT NULL
) ;
`

Am inserting two rows with json values.

INSERT INTO `TestInfo` (`Info`)
VALUES
    ('{
  "statusCode": 200,
  "result": {
    "summary": {
      "area": 0.0009904206008286565
    }
    }
} '
);

INSERT INTO `TestInfo` (`Info`)
VALUES
    (
'{
  "statusCode": 200,
  "result": {
    "summary": {
      "area": 0.0009904206008286565,
      "realty-society": {
        "price-min": {
          "property": "price-min",
          "min": 110000.00000000001,
          "max": 150000000,
          "average": 31184468.085106384,
          "sum": 1465670000
        }
      }
    }
  }
} '
);

When I run the query:

SELECT JSON_EXTRACT(Info, '$.result.summary') 
FROM TestInfo ;

it returns the 2 rows. This is fine. But when I run the same query with double quotes around the path like this:

SELECT JSON_EXTRACT(Info, '$."result.summary"') 
FROM TestInfo;

it returns the 2 rows (with the single column) as NULLs.

Ultimately I need to use double quotes for keys that have a hyphen (dash) in them. I am using MySQL 5.7 on AWS. Pl help.

0

2 Answers 2

1

Don't put double quotes around the whole path, just around a specific property name that contains special characters, e.g.

SELECT JSON_EXTRACT(Info, '$.result.summary."realty-society"."price-min"')
FROM TestInfo

Yuor code makes . part of the literal property name, rather than a separator between properties. You would use it if you had:

"result.summary": ...

in the object.

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

2 Comments

Tried the query you suggested with the double quotes only around "price-min". I get 2 rows with NULLs in the result.
It worked. The right query is 'SELECT JSON_EXTRACT(Info, '$.result.summary."realty-society"."price-min"') FROM TestInfo;'. Thanks a lot for the tip. Makes perfect sense.
1

This should be works if MYSQL version>=5.7

SELECT Info->>"$.result.summary" 
FROM TestInfo ;

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.