how to store special characters in json column in mysql - sql

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' => 'চাঁদপুর',
)

Related

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.

Splunk : formatting a csv file during indexing, values are being treated as new columns?

I am trying to create a new field during indexing however the fields become columns instead of values when i try to concat. What am i doing wrong ? I have looked in the docs and seems according ..
Would appreciate some help on this.
e.g.
.csv file
**Header1**, **Header2**
Value1 ,121244
transform.config
[test_transformstanza]
SOURCE_KEY = fields:Header1,Header2
REGEX =^(\w+\s+)(\d+)
FORMAT =
testresult::$1.$2
WRITE_META = true
fields.config
[testresult]
INDEXED = True
The regex is good, creates two groups from the data, but why is it creating a new field instead of assigning the value to result?. If i was to do ... testresult::$1 or testresult::$2 it works fine, but when concatenating it creates multiple headers with the value as headername. Is there an easier way to concat fields , e.g. if you have a csv file with header names can you just not refer to the header names? (i know how to do these using calculated fields but want to do it during indexing)
Thanks

How to store a Japanese Character in json_encode?

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

cakephp dealing with blob images

I am saving images as blob to my db, as I have to send them later on to an app via api call.
Saving works fine, but I can't select the row when the blob field is filled.
For example:
$this->Picture->find('list')
returns id and the name of the picture.
$this->Picture->find('all')
doesn't return anything at all!
I can see the right statement in the sql output, but the find does not return any value.
Not even an empty array, the debug($this->Picture->find('all')) is just empty.
When I delete the content of the blob field, the $this->Picture->find('all') returns me the right row with all it's data.
The field in my DB is BLOB.
What goes wrong here? Any ideas?
The statement look like this:
SELECT `Picture`.`id`, `Picture`.`picture`, `Picture`.`name`, `Picture`.`type`, `Picture`.`created`, `Picture`.`modified` FROM `app`.`pictures` AS `Picture` WHERE `Picture`.`id` = 13 LIMIT 1
If I run it with the phpmyadmin, there is one row found.
I use this code in my function:
public function show_image($pictureId) {
$this->set('data', $this->Picture->findById($pictureId));
}
the show_image.ctp looks like this:
$this->autoRender = false;
$this->response->type($data['Picture']['type']);
$this->response->body($data['Picture']['picture']);
I try to call the picture in my view like this:
$this->Html->image(Router::url(array('controller' => 'mycontroller', 'action' => 'show_image', $mydata['picture_id'])));