Error parsing query: Unexpected character \"'\"" Graph QL - json

I'm fairly new to GraphQL and I've been trying to call our graphQL server using postman with json format.
{"query":"query{stateQuery{avatar(address:'21638103') {action}}}"}
It always returns Unexpected character "'"" but when I try to use my GraphQL query below. It would query successfully.
query {
stateQuery {
avatar(address: "21638103") {
action
}
}
}

Unexpected character "'" means just what it says. It did not expect a '. Your example query that works uses "21638103" where the one that fails uses '21638103'. You need to use " instead of ' for strings.

GraphQL uses JSON ... JSON specs claims ...
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

Related

MySQL 5.7 JSON_EXTRACT does not work with quoted strings within the object: [ERROR] "Missing a closing quotation mark in string"

I am not able to extract quoted strings using JSON_EXTRACT function in MySQL 5.7.
Sample input:
{
"email": "d'souza#email.com",
"body": "I may have "random quotes '(single)/(double)" " in my source data"
}
Tried using,
SELECT
#valid_source := JSON_VALID(jsonString), NULL
IF(#valid_source, JSON_UNQUOTE(JSON_EXTRACT(jsonString, "$.email")), NULL)
I get an error stating: Invalid JSON text in argument 1 to function json_extract: "Missing a closing quotation mark in string." at position xxx
Any help will be appreciated, thanks!
Here is the fix that worked for me:
I used the operator "-->" instead of JSON_UNQUOTE(JSON_EXTRACT("jsonString")) and it did not throw me any error for any kind of quotes in my input string. Please note that the above Sample JSON was only one of the use cases that I was expecting in my input. I have around 4 million records, with all different combinations of characters and quotes since it contains the email bodies and using the operators instead of actual commands worked perfectly fine, it's weird since both are essentially the same but never the less I'm happy that I could resolve it using a small fix.
{
#valid_json := JSON_VALID(inputString),
IF(#valid_json, inputString ->> '$.email', NULL) AS EMAIL,
}
You are getting this error because the contents in the field, 'inputString' are invalid JSONs for some record. I think it's a mistake to blindly handle this error without persisting any error message for downstream / future users.
This approach may be acceptable (although I wouldn't accept this from a data engineer). A better solution would be to put a more informative error in the IF statement rather than a null, perhaps something like
CONCAT_WS(" ", 'INCORRECTLY FORMED JSON :', inputString), so that your overall function becomes
IF(valid_json(inputString), input_string ->> '$.email', CONCAT_WS(" ", 'INCORRECTLY FORMED JSON :', inputString)) as EMAIL.
That way any downstream users will be able to identify any underlying data quality issues that may be affecting the validity of conclusions. Along those lines, it may be worth having a completely separate field like
JSON_VALID(inputString) AS INPUT_JSON_VALID that you can use for easier downstream filtering.

Getting error while converting json file to a data frame using jsonlite

I am using the tweetscores package of R to get 'tweets list from twitter. The tweets are stored in json format. While converting it to a data frame I get a lexical error
' Error: lexical error: inside a string, '\' occurs before a character which it may not.".
Any solution to the mentioned error.
A part of the json file text
":[{"text":["MUFC"],"indices":[[83],[88]]}],"symbols":[],"user_mentions":[],"urls":[]},"metadata":{"iso_language_code":["en"],"result_type":["recent"]},"source":["http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>"],"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":[7.32108114527322e+017],"id_str":["732108114527322112"],"name":["wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww(^o^)/"],"screen_name":["SukiSukinal"],"location":["+6222"],"description":["Alliansi osaosi ngevote kagak. katanya sih fans a.k.a + "],"url":null,"entities":{"description":{"urls":[]}},"protected":[false],"followers_count":[163],"friends_count":[107],"listed_count":[4],"created_at":["Mon May 16 07:19:11 +0000
Json format does not allow backslashes so you need to escape them. replace any '\' character found with '\\'. Refer [here][1]
[1]: http://www.json.org/ for more info
You likely have an incomplete json string, which may be caused by the package or by an interrupted connection to Twitter's API. A complete json string returned from Twitter should look something like the following:
which I got using rtweet's stream_tweets() function. With a complete string returned by Twitter's REST or stream API, you should be able to convert the data using basically any json parser (e.g., jsonlite::fromJSON()).

Escaping Characters in Bigquery json_extract() function

when using Google's BigQuery, there's a function that can extract elements from json strings using jsonPath. For example:
SELECT JSON_EXTRACT(data,"$.key.value") AS feature FROM tablename
when the json key itself contains a dot,{"key.value":"value"} It's not clear how to escape that properly.
this jsonpath message board question says that jsonpath itself supports this format
#Test
public void path_with_bracket_notation() throws Exception {
String json = "{\"foo.bar\": {\"key\": \"value\"}}";
Assert.assertEquals("value", JsonPath.read(json, "$.['foo.bar'].key"));
However in bigquery this type of espcaping attempts cause Error: JSONPath parse error errors.
Update, new answer:
BigQuery's JSON_EXTRACT and JSON_EXTRACT_SCALAR functions now support JSON bracket notation in JSONPath, so the following query works:
SELECT JSON_EXTRACT('{"key.value": {"foo": "bar"}}', "$['key.value']")
and returns
{"foo":"bar"}
Old, now outdated answer:
Unfortunatelly BigQuery does not support escaping special characters in json path. The workaround would be to use REPLACE function to convert dots to underscores, i.e.
SELECT
json_extract(
replace('{"key.value":"value"}',
'key.value',
'key_value'),
'$.key_value')
use backtick to escape (it is also used to escape hyphen in project/dataset name)
SELECT JSON_VALUE(json_field.`key.value`) AS feature FROM tablename

How to convert string to BSON using MongoDB C++ driver?

Similar to toString is there a way we can convert a string to BSON object? I need to remove a document using C++ driver the the remove function expects the query to have BSON object.
Use the fromjson method found here:
http://api.mongodb.org/cplusplus/1.5.4/namespacemongo.html#a4f542be0d0f9bad2d8cb32c3436026c2
BSONObj mongo::fromjson ( const string & str )
Create a BSONObj from a JSON <http://www.json.org> string.
In addition to the JSON extensions extensions described here
http://mongodb.onconfluence.com/display/DOCS/Mongo+Extended+JSON, this function accepts
certain unquoted field names and allows single quotes to optionally be used when
specifying field names and string values instead of double quotes. JSON unicode escape
sequences (of the form ) are converted to utf8.
Exceptions:
MsgAssertionException if parsing fails. The message included with this assertion includes
a rough indication of where parsing failed.

JSON.stringify() not escaping apostrophe

...using JSON2.js and JQUERY
as you can see from the first image the object property customerReport.Title has an apostrophe. In the code you can see that I'm calling JSON.stringify() into reportAsJson string which still has the unescaped apostrophe.
the error returned by $.ajax() is {"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. ...
Initially I'm just going to ban apostrophe's from the user, but I thought JSON.stringify() handled this or do I need to set some option????
Thanks
You can avoid removing these apostrophes replacing them with an HTML entity ' - that's a single quot - and later decode HTML entities either in the client or server-side.
Following has worked for me after so many failed tries for stringify and other JSON parsing functions:
updatedString = string.replace(/('[a-zA-Z0-9\s]+\s*)'(\s*[a-zA-Z0-9\s]+')/g,"$1\\\'$2");
where
string = string having apostrophe in it.
updatedString = string with apostrophe issue resolved/escaped