Send JSON Object Inside MutipartFormData(FormData) - json

I have Formdata instance in component I use that to append my reactive form data.problem is I have an object that contains this fields {img:File,isCover:boolean} i want to append this to my form data.
problem is if i stringify this object the image field throws empty object {img:{},isCover:true}.when I dig a little bit about it. then saw if we stringify object with methods it will return an empty object
How do I overcome this.i want to send all those data in a single multipart data request.

To achieve what you want you need to append every value of your JSON object to a formdata.
First initialize a variable in you class.
formData = new FormData()
Then in your function append every json object.
this.formData.appen("yourKey", JSON.Object.Value)
It's kind of tedious though... You can , depending on your backend, nest all of the plain text objects in your JSON to a long string and append that to a FormData and call the key "JSON" for instance. And the append the image to the FormData with the key "Image".
After that you can in your API Backend get just the JSON stingified object with the key JSON and do something else with the image by getting it with the key "Image".

Related

Can I create a custom field with datatype as sobject in the Salesforce platform event?

In the platform event, I only see custom field datatypes with Text, Date, Check​box and Number, but not an sobject type. I need to serialize my own class inst​ance (which has accounts + contact list) and publish the object in json format.
I tried creating a custom text field (with name data__c) and assigned the json serialized object to this field. But, the receiver side is getting the unnecessary quote since it's a string. I want to avoid that and just publish as an object only, can I do that?
Now, at the receiver side the data__c is coming like start and ends with double quotes instead of just an object (which starts with curly brackets) like as follows:
enter image description here
So, one option is the receiver should take these quotes out and treat this as an object or other option sending it like an object itself. Request is, can we send as an object? Please help me here.
Thanks
// custom classes defined for json serialization
class CustomAccount {
String oper;
List<Account> accountList;
List<CustomContact> custContactList;
}
// creating an object
customAccount ca = new CustomAccount();
// My new platform event, with assigning the json serialized data to data__c which a text field
Pltf_Notifications__e eve = new Pltf_Notifications__e();
eve.data__c = json.serialize(ca);
enter code here
You can't create sobject data type for platform events. You can handle the logic from platform trigger if response has sobject type by deserializing the sobject data type. If it matches, you can call other handler classes from here by passing the sobject data and construct your desired format from here, then fires the platform event.

how to parse a json text on angular array of object .? and display it on ng 2 smart table

I'm doing the maintenance of an angular website, I have a problem of display in the ng2 smart table , in a column it displays the json of an object, this is the error that the developer has made by receiving the list of transaction where the user and items are a json text,
how can I parse them
ps this is the code :
this.data.doData('transaction','get').subscribe({
next:res=>{
console.log(res);
this.dataSource = [...res['transactions']]
console.log(this.dataSource)
},error:err=>{
console.log(err);
}
})
So actually you are getting the JSON string. To use it just do JSON.parse(items) and JSON.parse(user) and you will get the parsed JSON (array for the items and pbject for the user)
You can also use map in your service file and handle this parsing their so when you subscribe to the data in your component's ts file you will get the parsed form.
UPDATE:
Look at the quotes (quotes matching you would know from basic rules of strings, i.e. either single or double should be enclosing the main string). So when you were JSON parsing the string it wasn't taking it correct format to parse it.

Make a JSON object Without Quotes on the beginning and end of the string

I'm using React and fetch, and I'm trying to send a JSON string to a client's web service, they are expecting an object like this:
{
"id":"1",
"plan_id":"6",
"plan_start_date":"2017-08-02",
"months":"1",
"extra_hours":"4",
"attendees":"1",
"mails":"",
"shopping_cart_id":"0"
}
However, whenever I use JSON.stringify() to generate the JSON string, the result is something like this:
"{
"id":"1",
"plan_id":"6",
"plan_start_date":"2017-08-02",
"months":"1",
"extra_hours":"4",
"attendees":"1",
"mails":"",
"shopping_cart_id":"0"
}"
So when the request is sent, I get back an error stating that the object is not valid.
Is there a way to send the object like on the first example? I've tried manually building the object, but I can't get the key's names to stay in quotes.
EDIT: The code for the call is here:
addToCart(plan) { //Plan is the object with the previous example's structure
fetch("http:ClientWS", {
method: "POST",
body: JSON.stringify(plan) //Produces the "{}" issue
})
.then(response => response.json())
.then(json => {
//Read the response info, here it tells me that the value for 'id' is invalid
console.log(json.datos);
}).catch(function(ex) {
console.log(ex);
});
}
JSON.stringify() does not add extra quotes around the output. If you are seeing those extra quotes, I would say that it implies that the error is somewhere else.
That's because JSON.stringify takes your object and outputs a string variable by concatenating all the properties and values together and inserting some separators. And a string is displayed in quotes, again because it's a string. It's not an object any more. When you send it to the server, the server will see it as a single string value. The fact that the content of the string happens to look like JSON is not taken into consideration.
You do know that JSON and JavaScript objects are essentially the same thing? The stringified JSON that you can see is just a human-readable version of the object structure. It also turns out to be a convenient format in which to serialise objects (from all languages not just JS) for transmission in a HTTP request (or storage in a file or database, among other uses).
So actually when you send your object to the server, do just that - send the object. If you're doing it via ajax, the JS code you're using will generally serialise it for transmission on your behalf. You may have to set the correct content type header. That serialised object inside the HTTP request's body will end up looking a lot like the output of JSON.stringify (if you viewed it in your browser's network tab, for instance), except it won't actually be a string, and the server will interpret it correclty as an object containing multiple properties.

Serialize Object with PersistentCollection To JSON

I want to serialize entity object in my Symfony2 application to JSON object (I have to pass it to ajax function and use it in my javascripts).
Everything works fine but I have in my entity object addresses which are PersistentCollection object. Then if I serialize them in normal way field "adresses" has got empty Object. I figured out that I can set "LimitedRecursiveGetSetMethodNormalizer" to "1" and then PersistentCollection is being serialized. The problem is that I've got this object serialized, not array with my addresses so I can't use them in my javascript because they are not there...
\Cloud\ApplicationBundle\Resources\LimitedRecursiveGetSetMethodNormalizer::$limit = 1;
$businessJson = $this->get('serializer')->serialize($business, 'json');
Variable $business is of course Entity Object. I hope that my question is clear.
I have to add that I know that i can convert PersistentCollection object to Array and then serialize it to Json but this way i would have to pass my entity and adresses in seperate variables. I would prefer to do it one variable.
Thanks for any help !

Deserialize a string to json

I want to know whether deserialize converts json to string or string to json.I need my string to be returned as Json so i used deserialize, but unsure about its syntax.Can anyone direct me correctly.
My code
JavaScriptSerializer datajson = new JavaScriptSerializer();
var objec = datajson.Deserialize<string>(data);
return Json(objec,JsonRequestBehavior.AllowGet);
Serialisation is the act of taking objects and turning them into something more persistable or communicable, i.e. turning objects into JSON, XML or binary data.
Deserialisation is the act of taking serialised data and turning it back into objects.
So in your case, if you want to turn your objects/variables into JSON, the process is called serialisation.
Your code, assuming it is MVC C# (you may wish to add these tags to your original post), appears to be deserialising a JSON encoded string into a string, then serialising it back to JSON again when it returns the view. I'm not sure why you would want to do this. You should be able to simply do:
return Json(data, JsonRequestBehavior.AllowGet);