Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
Answer from JonoW on Stack OverflowScript for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
I made it that way:
if I have:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
» npm install array-to-json
Your data is insode Photos object, and then you push in an empty array.
So when you find from the array, you need to get it from the correct position.
Correct code:
(function test(id) {
let PhotoData = "{\"Photos\":[{\"id\":\"Photo1\", \"Type\": \"Color\", \"Shade\": \"Grey\"}, {\"id\": \"Photo2\", \"Type\": \"Color\", \"Shade\": \"Red\"}]}"
let photoArray = []; //photoArray
let getPhoto = JSON.parse(PhotoData); //Converting to JSON object
console.log(getPhoto);
photoArray.push(getPhoto); //Pushing to arrayv so that search can be done
console.log(photoArray);
const foundPhoto = photoArray[0].Photos.find((photo) => photo.id == id);
console.log(foundPhoto );
})("Photo1");
Result:
{id: "Photo1", Type: "Color", Shade: "Grey"}
It's strange that you convert this object with photos back to a string with JSON.stringify() and then add that string to an array. See my suggestion below.
const existingArrayOfPhotos = [];
yamlString = `Photos:
- id: Photo1
Type: Color
Shade: Grey
- id: Photo2
Type: Color
Shade: Red`
const photoData = YAML.parse(yamlString);
existingArrayOfPhotos.push(...photoData.Photos)
const foundPhoto = existingArrayOfPhotos.find((photo) => photo.id == "Photo1");
console.log(foundPhoto);
<script src="https://cdnjs.cloudflare.com/ajax/libs/yamljs/0.3.0/yaml.min.js"></script>
Build up a JavaScript data structure with the required information, then turn it into the json string at the end.
Based on what I think you're doing, try something like this:
var result = [];
for (var name in goals) {
if (goals.hasOwnProperty(name)) {
result.push({name: name, goals: goals[name]});
}
}
res.contentType('application/json');
res.send(JSON.stringify(result));
or something along those lines.
You don't have JSON. You have a JavaScript data structure consisting of objects, an array, some strings and some numbers.
Use JSON.stringify(object) to turn it into (a string of) JSON text.