How to store a Japanese Character in json_encode? - sql

I'm trying to save a json_encoded strings to the database but whenever i do i get this:
u30abu30bf
when saving:
カタ
how do i decode such a thing?
tried php utf8_decode and encode but i doesn't seem to work,
i also tried reading the threads below:
PHP decoding and encoding json with unicode characters
difficulty passing Japanese characters(UTF-8) via json_encode
here is a var_dump data of $flex_encoded before passing it as a parameter:
//this one seems to passing the right encoding
[{"japanese_name":"\u30ca\u30ab\u30cd"},{"japanese_name":"\u30ca\u30ab\u30cd"}]
here is the function call:
$this->updateFlexData($game_id, $flex_encoded);
here is the function for updating my db:
function updateFlexData($game_id, $json_data)
{
$arrVal = array(
'json_data'=> $json_data,
);
Db::getInstance()->autoExecute('japanese_names', $arrVal, 'UPDATE', " game_id = '".$game_id."'", false);
}
database column type is:
`json_data` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
my PHP version:
PHP Version 5.3.10

Try JSON_UNESCAPED_UNICODE
json_encode($data, JSON_UNESCAPED_UNICODE);
And instead of this \u7537\u6027\u304c\u5f7c\u5973\u306b\u7740\u3066\... you're going to get 男性が彼...

after hours of tinkering with the code i somehow made it to work:
ok its maybe not the solution but:
when i proceed to storing json_data with this content(below) decoding json_data from database results to parse error because of the extra " quotations
$japanese_name = json_encode($japanese_text); //returns "\u30ca\u30ab\u30cd"
i just removed the " from each sides to make it just \u30ca\u30ab\u30cd and then proceeded to store in the db
$jp = str_replace("\"","",$japanese_name); //returns \u30ca\u30ab\u30cd

Related

unable to convert raw data to json format after replacing the \(slash)

I am passing the raw data and removing unwanted slash and trying to convert to json format, unable to proceed.
json spot = {"LoginDetails":"{\"firstName\":\"abcd\",\"lastName\":\"\",\"middleName\":\"\",\"phoneNumber\":\"6944000000\",\"phoneCountryCode\":\"+91\",\"dob\":\"1945-02-22\"}"}
string a = spot
def ab = a.replace("\\", "")
json st = ab
And request st
When method POST
Then status 200
Getting error
net.minidev.json.parser.ParseException: Unexpected token f
How to resolve this issue and convert the above payload into json format?
NOTE: After conversion to json firstname, phonenumber should be passed dynamically from the preceding API calls.
If u want escape slash with json, you could try
a.replaceAll("\\", "\\\\")
or change it to unicode.
a.replaceAll("\\", "\u005C")
Both of them is workable for browser but not sure for karate, maybe you need change a bit by yourself.

How do you convert text column data with Ruby JSON format ("key" => "value") to standard JSON?

I have data in the comment column of the payments table. The data is stored as plain text in the following format:
{"foo"=>"bar"}
I need to query the value of the specific "foo" key and tried the following:
select comment::json -> 'foo' from payments
but because the data stored is not in JSON format I get the following error:
invalid input syntax for type json DETAIL: Token "=" is invalid. CONTEXT: JSON data, line 1: {"foo"=>"bar"}
which refers to the => that Ruby uses for Hashes.
Is there a way to convert the text data to JSON data on-the-fly so I can then access the specific keys I need?
You can replace the => with a : to make that example a valid JSON value:
replace(comment, '=>', ':')::jsonb ->> 'foo'
It sounds like the data is technically valid ruby which means we can do something a bit clever.
require 'json'
def parse_data(data_string)
eval(data_string).to_json
end
Should do the trick so long as the data is trusted.

ORDS: Removing escaping char from json in ORDS OUT param

I am using the OUT parameter in the ORDS parameters to send a response which is already in JSON and this stored as CLOB in the DB. When I send this out in the response from the ORDS, ORDS is actually adding lot of back spaces to the response.
Can someone help me to understand how I can remove the escaping chars here(all the backslashes). I had tried a different approach explained by Jeff in another thread to try to use an alias for the JSON key, but it did not work for me.
Over here I have mentioned the response as OUT parameter in my code.
eg payload:
{
"response": "{\n\"Order\":{\n\"OriginalShipmentID\":1\n,\"orderNumber\":1\n,\"orgID\":1\n,\"orderShipmentNumber\":1\n,\"oracleShipSet\":\"1\"\n,\"OeHeaderId\":1\n,\"GsHeaderId\":1\n,\"CustomerPoNumber\":\"1\"\n,\"PaymentTerms\":1\n,\"FreightTerms\":\"PAID\"\n,\"CurrencyCode\":\"USD\"\n,\"BillToSiteUseID\":1\n,\"ShipToSiteUseID\":1\n,\"SalesChannel\":\"DIRECT\"\n,\"HeaderKeyCode\":\"1 1\"\n,\"OrigSysDocumentRef\":\"1\"\n,\"OrderTypeCode\":\"XYZ yXYZ\"\n,\"OrderTypeID\":1\n,\"SalesRepID\":-3\n,\"IsAtgOrder\":\"N\"\n,\"OrderSource\":\"MCP\"\n,\"OdiDocSet\":\"PO\"\n,\"CsServiceSymbol\":\"XYZ\"\n,\"CsServiceFriendlyName\":\"XYZ\"\n,\"CsShipper\":\"NDC_MX\"\n,\"GsShipToAddressID\":1\n,\"GsBillFrtAddressID\":-1\n,\"BillingMethod\":\"DoNotBillFreight\"\n,\"ReasonForNoInvoice\":\"NA\"\n,\"ShippedBy\":\"NA\"\n,\"ManifestID\":1771748\n,\"IsVoided\":\"N\"\n,\"CustomerNumber\":\"1\"\n,\"OrderLines\":[\n]\n,\"HeaderShortNotes\":{\n}\n}\n}\n"
}
Some more details:
SELECT get_json ( :ordernumber,
:warehouseid,
:shipset,
:ordershipmentno,
:gsshipmentid,
:countrycode,
:shipperid,
:fromcurrency,
:tocurrency,
:groupid,
:optionvalues,
:linehaul,
:servicename,
:sigid,
:lineid,
:customerid) "{}Order"
INTO l_response_clob
FROM DUAL;
This function returns a CLOB with a JSON format data and l_response_clob is defined as STRING output parameter on the ORDS.
Essentially, I want to stop ORDS from again converting JSON to JSON.
Been banging head over this from some time but can't seem to make it work.
Thanks for the help I can get here.

how to store special characters in json column in mysql

Im am trying to save some special characters in a json column of a table. but this is actually not working
this is how it is stored
{"district":"\u099a\u09be\u0981\u09a6\u09aa\u09c1\u09b0",
"sub_district":"\u099a\u09be\u0981\u09a6\u09aa\u09c1\u09b0"}
i am storing my input values as below..i am using laravel
present = array('district'=>$request->district,'sub_district'=>$request->sub_districtce);
$card->present_address = json_encode($present);
and while searching for a string in that json object, i am using a query below
$allowances = Card::SELECT('id','name','nid','village')
->where(DB::raw("json_extract((present_address), '$.district')"), চাদপুর)
->get();
can anybody help me on this situation where i can store special characters/unicodes in that json object.?
It is perfectly normal. As soon as you will do json_decode(), you will get back your expected values:
array (
'district' => 'চাঁদপুর',
'sub_district' => 'চাঁদপুর',
)

Lucene query : parse execption

I am using alfresco and trying to execute these queries,
These are my queries.
1st:
PATH:"/app:company_home/st:sites/cm:swsdp/cm:dataLists/cm:aea88103-517e-4aa0-a3be-de258d0e6465//*"
1st query is working properly but 2nd query is not able to parse
2nd
+PATH:"/app:company_home/st:swsdp/cm:/cm:dataLists/cm:9787a75b-cbc9-4d42-b76c-df88461e62c6//*"
Exception :
Cannot parse '+PATH:"/app:company_home/st:swsdp/cm:/cm:dataLists/cm:9787a75b-cbc9-4d42-b76c-df88461e62c6//*" AND +TYPE:"fdm:formDatalist"': Failed to parse XPath...
Unexpected '9787'
I tried by escaping but still getting same.
+PATH:"/app:company_home/st:swsdp/cm:/cm:dataLists/cm:9787a75b\-cbc9\-4d42-b76c\-df88461e62c6//
I noticed that in 1st query cm:aea88103-517e-4aa0-a3be-de258d0e6465 starts with latter but in 2nd query cm:9787a75b-cbc9-4d42-b76c-df88461e62c6 this contains numbers in starting so that it's not able to parse.
Please solve this error.
Certain characters need to be encoded in hexa for lucene PATH queries.
You need to encode your path this way :
var rawString = "//test:123 DIR/FILE.TXT #";
=> rawString: //test:123 DIR/FILE.TXT #
var encodedString = search.ISO9075Encode(rawString);
=> encodedString: _x002f__x002f_test_x003a_123_x0020_DIR_x002f_FILE.TXT_x0020__x0040_
var decodedString = search.ISO9075Decode(encodedString);
=> decodedString: //test:123 DIR/FILE.TXT #
See alfresco documentation for more information : http://docs.alfresco.com/5.2/references/API-JS-iso9075Encode.html