question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Json for special key

See original GitHub issue

Hello, I have some data like

{
   "318":{
      "cid":8,
      "xPath":"//*[@id=\"app\"]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/p[411]"
   },
   "329":{
      "cid":7,
      "xPath":""
   }
}
var readPostion = await _context.UserData.Where(x => x.UserId == userId)
    .Select(x => x.ReadPosition[bid.ToString()])
    .FirstOrDefaultAsync();

ReadPosition is JObject, bid is int

They generated sql statements like this

SELECT JSON_EXTRACT(`u`.`read_position`, '$.318')
FROM `user_data` AS `u`
WHERE `u`.`user_id` = 2
LIMIT 1

and throw an error Invalid JSON path expression. The error is around character position 5.

The correct one should be like this

SELECT JSON_EXTRACT(`u`.`read_position`, '$."318"')
FROM `user_data` AS `u`
WHERE `u`.`user_id` = 2
LIMIT 1

Is this a bug, or am I missing something?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
wuyu8512commented, Dec 11, 2021

Please provide which version of Pomelo.EntityFrameworkCore.MySql you’re using. It should have been stated that you configured Pomelo.EntityFrameworkCore.MySql.Json.Newtonsoft for JSON support.

I suspect that the ReadPosition argument doesn’t support expressions. Try defining a variable beforehand to cast bid to a string and use that instead.

my Pomelo.EntityFrameworkCore.MySql is 6.0.0

var userId = _user.GetUserID();
var bidString = bid.ToString();
var readPostion = await _context.UserData.Where(x => x.UserId == userId)
    .Select(x => x.ReadPosition[bidString])
    .FirstOrDefaultAsync();

the result is same

my config

services.AddDbContext<BookShelfContext>(options =>
{
    options.UseMySql(connectionString, serverVersion,
        builder =>
        {
            builder.EnableRetryOnFailure();
            builder.UseNewtonsoftJson(MySqlCommonJsonChangeTrackingOptions.FullHierarchyOptimizedFast);
        });
    if (Env.IsDevelopment())
    {
        options.EnableSensitiveDataLogging();
        options.EnableDetailedErrors();
        //options.LogTo(Console.WriteLine, LogLevel.Debug);
    }
});
0reactions
wuyu8512commented, Dec 17, 2021

This is a long shot, but you could try x.ReadPosition.GetValue(bidString) or x.ReadPosition.Root[bidString] also.

hi,x.ReadPosition.Root[bidString] has same problem

x.ReadPosition.GetValue[bidString] is ok , but he generated such sql statement

SELECT `u`.`read_position`
FROM `user` AS `u`
WHERE `u`.`id` = @__userId_0
LIMIT 1

Cannot query in sql statement

Read more comments on GitHub >

github_iconTop Results From Across the Web

Which characters are valid/invalid in a JSON key name?
For JSON, the characters required to be escaped are the quotation mark, backslash, and control characters. For Javascript, the characters ...
Read more >
Valid Key Names in JSON - REST
JSON Keys must be Valid Strings ... According to JSON.org, a string is a sequence of zero or more Unicode characters, wrapped in...
Read more >
Accessing a JSON object with special Characters
Hi, I'm trying to access a specific value and the JSON object has a special character (@) and I'm lost about how to...
Read more >
Find and replace all special characters in all json keys
Find and replace all special characters in all json keys. So I have a new line delimited JSON { "foo-bar":"foo-baar", "foob@r":"bar.foo", ...
Read more >
How to Reference JSON Keys that Contain Special ...
How to Reference JSON Keys that Contain Special Characters when using OPENJSON, JSON_QUERY, and JSON_VALUE (SQL Server).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found