How to separate JSON data from ArrayBuffer receiving from websocket - javascript

I tried unsuccessful to separate JSON data from an array of buffer receiving from websocket which look like this.
{"type":"string","data":{"UklGRkIjAABXRUJQVlA4IDYjAACQswCdASqrAfAAPm0wlUemI"}}
[object ArrayBuffer]
the objective is to read them separately with proper function. My old method is parse the JSON first. If fail, pass through another function.
The reason I send them in both formats is that converting JSON data to an array or array to JSON would increase file size around three fold.
The best practice is to send them separately.
However, at the terminal I read JSON data with
var json = JSON.parse(e.data);
and read the ArrayBuffer with DataView method.
The app works properly on the surface, but if you check at console.log there are too many Uncaugth Error.
It also blocks data flow at one point, causing the stream overflow not very smooth.
Thanks for any suggestions in advance.

Got it:
if ((typeof data == 'object')) {
// this would return ArrayBuffer
} else {
// this would return JSON
}

Related

API Connect - 500 error when including basic Javascript

I'm trying some basic API Connect tutorials on IBM's platform (running locally using loopback) and have got completely stuck at an early point.
I've built a basic API service with some in-memory data and setter / getter functions. I've then built a separate API which takes two GET parameters and uses one of my getter functions to perform a search based on two criteria. When I run it, I successfully get a response with the following JSON object:
[{"itemId":1,"charge":9,"itemSize":2,"id":2}]
I've then tried to add a piece of server logic that modifies the response data - at this point, I'm just trying to add an extra field. I've added a Javascript component in the Assemble view and included the following code (taken from a tutorial), which I thought should modify the message body returned by the API while still passing it through:
//APIC: get the payload
var json = apim.getvariable('message.body');
//console.error("json %s", JSON.stringify(json));
//same: code to inject new attribute
json.platform = 'Powered by IBM API Connect';
//APIC: set the payload
//message.body = json;
apim.setvariable('message.body', json);
Instead of getting an extra JSON parameter ("platform"), all I get is a 500 error when I call the service. I'm guessing that I'm doing something fundamentally wrong, but all the docs suggest these are the right variable names to use.
You can't access json.platform but at that point json variable is json type. Are you sure that you can add a property to a json type variable if your json object lacks of that property? I mean: What if you first parse the json variable of json type to a normal object, then add new property, and finally stringify to json type again for body assigning purposes?
var json = JSON.parse(apim.getvariable('message.body')); //convert to normal object
json.platform = 'Powered by IBM API Connect'; //add new property
apim.setvariable('message.body', JSON.stringify(json)); //convert to json again before setting as body value
You need to get the context in some determined format, and in this function do your logic. For example if your message is in json you need to do:
apim.readInputAsJSON(function (error, json) {
if (error)
{
// handle error
apim.error('MyError', 500, 'Internal Error', 'Some error message');
}
else
{
//APIC: get the payload
var json = apim.getvariable('message.body');
//console.error("json %s", JSON.stringify(json));
if(json){
//same: code to inject new attribute
json.platform = 'Powered by IBM API Connect';
//APIC: set the payload
//message.body = json;
apim.setvariable('message.body', json);
}
}
});
Reference:
IBM Reference
You have the message.body empty, put a invoke/proxy policy before your gateway/javascript policy for example.

How to retrieve data from DB and turn them to object in my case?

I am using postgres row_to_json function to get the data that were stored through JSON.stringify(). However, when I retrieve it and do JSON.parse(), it gave me "unexpected token ," error message.
My original data from frontend:
{
"company":[
{"name":"test company"},
{"ceo":"John"}
]
}
I do JSON.stringify(myData.company) and store it to postgres.
When I retrieve it, I have row_to_json function to get the data.
and the console.log(myRetrieveData) become
{"{\"name\":\"test company\"}","{\"ceo\":\"John\"}"}
I used JSON.parse(myRetrieveData) and got 'unpexted token ,' error.
I understand row_to_json turn it into json and can't be used on JSON.parse but the function is needed for my other data.
I am not sure how to fix this. Can anyone help me about it?
Thanks a lot!
Looks like the JSON that you retrieved from the query is not of the proper format for it to be parsed. Inorder to avoid this please make sure that the JSON being stored into the DB is of proper JSON format, so that it parsed.If this is not achievable the response obtained from the db has to be modified to proper JSON. With the above data it can be done as follows.
var improperJson = '{"{\"name\":\"test company\"}","{\"ceo\":\"John\"}"}';
var properJson = improperJson.replace(/","/g, ",").replace(/^{"/, "[").replace(/"}$/, "]");
console.log(JSON.parse(properJson));//should give you the parsed JSON

Accessing JSON data when response contains both JSON data and text

Initially the content from the server was JSON data.And I was able to access the data perfectly.
{ "status":"ok", "artifact":"weblayer-war", "version":"0.0.41-test-data", "buildtime":"test-data" }
Now the response from the server has been changed to json data + text data.
{ "status":"ok", "artifact":"weblayer-war", "version":"0.0.41-test-data", "buildtime":"test-data" }
Properties for service:
=======================
ServiceEndpoint: https://somedomain:1200/web/Servlet/SOAP/Services
Certificates: false
DocumentName: note.pdf
So whether changing the content-type to application-text and using split method is the only way we can solve this or Is there any better approach?
As mentioned in the comments, mixing JSON and pure text data is wrong (as it defeats the advantage of using standardized data format) and should be avoided. Maybe that is some kind of debug log that was accidentally included in the production code?
If not, you should read just the first line of response and parse it as JSON data, ignoring the rest, hoping for no more changes in response structure ;)
You really should overthink your API design.
What about adding the text part as part of your JSON?
{
... // your JSON properties here
"serviceProperties": { // properties for service
"ServiceEndpoint": "https://somedomain:1200/web/Servlet/SOAP/Services",
"Certificates": false,
"DocumentName": "note.pdf"
}
}

How upload large JSON object in javascript with form data?

I'm trying to upload a very large object that contains several arrays within which are more arrays. I tried to upload using FormData but the upload is received as a string, like so:
Upload code:
formData.append('large_json', largObj);
As received on server:
{ "bob" : "[[Object object], [Object object]]" }
The string may be 15 million characters long. What are my options to get this to the server?
If it helps, I'm running an express server.
I think you are sending the object to the server, not the info in it.
Did you already try to transform your object to JSON?
formData.append('large_json', JSON.stringify(largObj));

Parse serialized Laravel cache data with Javascript

I'm using Laravel's cache feature to store keys in Redis. The key is stored as an array which Laravel's cache automatically serializes and unserializes into something like this:
"s:18:\"[\"bob\",\"dave\"]\";"
In PHP Laravel handles the parsing, but I need to access the data from Javascript. I am using the ioredis package in node. Everything works fine except for unserializing the data.
var Redis = require('ioredis');
var rediscache = new Redis();
rediscache.get("mykey", function (err, result) {
//Attempt to parse?
result = JSON.parse(result);
}
The key is fetched correctly, but is stuck as a string. I tried JSON.parse but this throws an error, I suppose because the format is wrong.
Unexpected token s at Object.parse (native)
How can it be correctly unserialised? I ideally want to get it as a Javascript array so a value can be altered, then re-serialised and saved back to Redis.
Many thanks.
What you have is a serialized string which contains a JSON string.
You can use a library if you don't want to write it yourself: https://github.com/naholyr/js-php-unserialize
After that, you'll have a JSON string, which you can then parse.

Categories

Resources