How to convert array of images (from input field) into json string - javascript

I have an array of images created with
var a = Array.from(document.getElementById("id").files);
then I want to make a JSON string of that array with
var b = JSON.stringify(a);
but it produces nothing but an empty array. As I read this is a common problem with stringify but I couldn't find a solution.

Assuming your array of images are proper File objects, they are a type of binary data called blob.
JSON.Stringify won't work out of the box with blobs, but here is an answer with some workarounds: JSON.stringify or how to serialize binary data as base64 encoded JSON?

Related

transform array of strings to array of objects

I have an array of strings (strings have an object shape). I would like to convert it to an array of objects.
I tried with JSON.parse but am not successful.
let a=["{Axis:1,Value:-74}", "{Axis:2,Value:7}", "{Axis:3,Value:-47}", "{Axis:4,Value:85}"]
Desired result
a=[{Axis:1,Value:-74}, {Axis:2,Value:7}, {Axis:3,Value:-47}, {Axis:4,Value:85}]
You could evaluate the objects, because they are not formatted as JSON standard.
let a = ["{Axis:1,Value:-74}", "{Axis:2,Value:7}", "{Axis:3,Value:-47}", "{Axis:4,Value:85}"],
result = a.map(s => eval(`(${s})`));
console.log(result);
There are a couple of issues here.
The first is that you won't be able to call JSON.parse on a because JSON.parse can only parse strings containing properly formatted JSON.
The second issue is that the strings in your array are not properly formatted JSON.
The solution for this would be to make the strings properly formatted JSON, as follows:
let a=['{"Axis":1,"Value":-74}", '{"Axis":2,Value:7}', '{"Axis":3,"Value":-47}', '{"Axis":4,"Value":85}'];
Then to create an array of those objects, you'll have to use Array.map, as follows:
a = a.map(e => JSON.parse(e));

How to Convert array of Json

var x=[{"name":"james","age":"23"},{"name":"job","age":"55"}];
How to convert array of json object to below response
{{"name":"james","age":"23"},{"name":"job","age":"55"}};
let x=[{"name":"james","age":"23"},{"name":"job","age":"55"}];
console.log(JSON.stringify(x));
The desired output isnt proper JSON. To convert any javascript object into a json string simply use the function JSON.stringify(obj). JSON.parse(string) can be used to create an object out of a string.

how can i take json data and read it into javascript object?

how can i take this json data (either from website or pasted into local file) and read it into a javascript object?
Here is the json data: https://jsonblob.com/b914b62a-e276-11e7-8a9c-f99a86745e2b
You can convert an JSON file to an object by using JSON.parse
let convertedObject = JSON.parse(text)
There are not many differences between a JSON string and a Javascript Object. In fact Everything is an object in javascript, from functions to arrays...well coming back to your question you can get the value stored in a JSON string by parsing the JSON string by using JSON.parse() function and then call it as you generally do with JS objects. for Example: -
var jsonString = JSON.stringify({key: "val"}); // here i am converting a regular object to JSON string
var jsonToObj = JSON.parse(jsonString);
console.log(jsonToObj.key);

Accessing Elements of JS Object

I have created array in a object,
var obj_report_dailog = { array_report_dailog : [] }
Then push data to object,
obj_report_dialog.array_report_dialog.push({from: fromDate})
obj_report_dialog.array_report_dialog.push({to: toDate})
obj_report_dialog.array_report_dialog.push({fabrika: fabrika})
Then,
var json = JSON.stringify(obj_report_dialog);
How can I access to elements of that object?
console.log("işte bu: " + json);
output:
işte bu: {"array_report_dialog":[{"from":"2017-08-01"},{"to":"2017-09-21"},{"fabrika":["Balçova"]}]}
Two things:
You don't want to JSON.stringify unless you're sending the resulting string somewhere that will parse it. Remember, JSON is a textual notation for data exchange; JSON.stringify gives you a string to send to some receiver. When you have the JSON string, you don't access the properties of the objects in it.
If you're receiving that JSON string, you'd parse it via JSON.parse and then access the properties on the result.
Leaving aside the JSON thing, you probably don't want to add data the way you're adding it. You're adding three separate objects as three entries in the array, each with one property. You probably want to push one object with all three properties:
obj_report_dialog.array_report_dialog.push({
from: fromDate,
to: toDate,
fabrika: fabrika
});
Then you'd access them as obj_report_dialog.array_report_dialog[0].from, obj_report_dialog.array_report_dialog[0].to, and obj_report_dialog.array_report_dialog[0].fabrika. Or more likely, you'd have a loop like this:
obj_report_dialog.array_report_dialog.forEach(function(entry) {
// Use entry.from, entry.to, and entry.fabrika here
});
(See this answer for more options for looping through arrays.)
But, if you really want to push them as separate objects, you'd access them as obj_report_dialog.array_report_dialog[0].from, obj_report_dialog.array_report_dialog[1].to, and obj_report_dialog.array_report_dialog[2].fabrika (note the indexes going up).
If you stringify a json, it's gonna create a string representation of your object.
To access data in a string like that we usually use JSON.parse that create an json object from a string. Which is the obj_report_dailog you had at start.
You can make object from json by using JSON.parse()
JSON.parse(json).array_report_dialog

How to convert a string to JSON format?

I was getting list in array form. So at first place i converted array list to string-
var myJsonString = JSON.stringify(result);
myJsonString="[{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},
{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}]"
But again i need to convert myJsonString to Json format, What i need to do? I mean i need to replace 1st" and last ", I guess
You need to call parse now.
JSON.parse(myJsonString)
First, if you ever find yourself building a JSON string by concatenating strings, know that this is probably the wrong approach.
I don't really understand how the first line of your code relates to the second, in that you are not doing anything with JSON-encoded string output from result, but instead just overwriting this on the following line.
So, I am going to limit my answer to show how you could better form JSON from an object/array definition like you have. That might look like this:
// build data structure first
// in this example we are using javascript array and object literal notation.
var objArray = [
{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}
];
// now that your data structure is built, encoded it to JSON
var JsonString = JSON.stringify(objArray);
Now if you want to work with JSON-encoded data, You just do the opposite:
var newObjArray = JSON.parse(JsonString);
These are really the only two commands you should ever use in javascript when encoding/decoding JSON. You should not try to manually build or modify JSON strings, unless you have a very specific reason to do so.

Categories

Resources