I´d like to create a JSON-Object for a Google API-Request. Only content is needed to change. My solution gives me an invalid JSON-Format and is more a hack. Is there an easier way to do this? Thank your for your hints.
The necessary format look like this:
{
"requests": [
{
"image": {
"content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
},
"features": [
{
"type": "DOCUMENT_TEXT_DETECTION"
}
]
}
]
}
JS
var cvs = cvs.substring('data:image/png;base64,'.length);
var json1 = '{"requests":[{ "image":{ "content":"'
var json2 = '"}, "features": [{"type":"DOCUMENT_TEXT_DETECTION"}] } ]}'
var entireJson = json1 + cvs + json2;
var ocrImage = JSON.stringify(entireJson);
What you have done in your example is initializing a Javascript Object.
JSON.parse(object_string); is not necessary. You may initialize it directly:
var ocrImage = {
"requests": [
{
"image": {
"content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
},
"features": [
{
"type": "DOCUMENT_TEXT_DETECTION"
}
]
}
]
}
console.log(ocrImage)
Related
I am trying to validate the format of the data request call in postman. The data is of type uuid and relates to property id (shown below).
Would someone please be able to help with this?
This is my code:
var jsonData = JSON.parse(responseBody);
var Ajv = require('ajv'),
ajv = new Ajv ({logger: console}),
schemaResponse = {
"required": [
"id",
"ID",
],
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuidFormatCheck"
}
};
Code wise it is like this:
var currentSchPmExpTest;
pm.test('Schema is valid', function() {
//var data = pm.response.json();
ajv.addFormat('uuidFormatCheck', /^[0-9e-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
currentSchPmExpTest = "schemaResponse variable";
pm.expect(ajv.validate(schemaResponse, jsonData)).to.be.true;
});
The body response is:
{
"sfsid": "11c22abc-c11a-1df2-ba3a-123a456b78f1",
};
Given the example response body you provided:
{
"sfsid": "11c22abc-c11a-1df2-ba3a-123a456b78f1"
}
You could use AJV in Postman like this:
let schemaResponse = {
"type": "object",
"required": [
"sfsid"
],
"properties": {
"sfsid": {
"type": "string",
"pattern": "^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"
}
}
};
pm.test('Schema is valid', function() {
pm.response.to.have.jsonSchema(schemaResponse)
});
I'm using Three.js to save and load objects in a database. I'm inserting my objects as JSON.
The problem is that JSON.stringify or toJSON() transform the image url (textures) in base64, and I wanted to keep the http url.
This is the originial JSON:
{
"metadata": {
[...]
},
"geometries": [
{
[...]
}],
"materials": [
{
[...]
}],
"textures": [
{
[...]
}],
"images": [
{
"uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
"url": "data:image/png;base64,iVBORw0KGgoATyuRZGZVREd0FAERRRBAEBEFwAYEyKqP/Rfozx+5fDYjvpeT/akv+n/J+/7eMjX9Ke8uojP4JVAYAyuj/Ld3Por7fPQ9i8d7vvr8LKNzLg/dn1u1hvQf3q/v92vRX0X/ [...]"
}],
"object": {
[...]
}
}
And the desired kind of JSON would be this :
{
"metadata": {
[...]
},
"geometries": [
{
[...]
}],
"materials": [
{
[...]
}],
"textures": [
{
[...]
}],
"images": [
{
"uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
"url": "http://www.domain.com/picture/path.png"
}],
"object": {
[...]
}
}
Is there a solution to do this ?
After studying the JSON file and getting my texture path, I did the following:
var objectParsed = SELECTED.toJSON(); //transform the selected object in JSON
textureBase64 = objectParsed.images[0].url; //get the value of the base64 encoded image
objectParsed.images[0].url = texMap; // replace the base64 encoded image with the http texture path
objectParsed = JSON.stringify( objectParsed, null, '\t' );
objectParsed = objectParsed.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' ); // make the JSON readable
And now it did the trick ! :)
I have a json data
var data={"mainVariants": [
{
"header": "Measure"
},
{
"header": "Disposal"
},
{
"header": "Stairs"
},
{
"header": "Furniture"
}
],
"Measure": {}}
var val1=data.mainVariants[0].header;//returned as "Measure"
Now i want to retrieve data.val1.Pls help
Try this
var val1 = data.mainVariants[0].getString("header");
You can just do var obj = data[val1]
I have JSON data like this
{
"nodes": [
{
"node": {
"title": "Kieu Oanh",
"Image": "",
"view_node": "view",
"link": "drupal_dev/content/kieu-oanh"
}
},
{
"node": {
"title": "Kieu Oanh",
"Image": "",
"view_node": "view",
"link": "drupal_dev/content/kieu-oanh"
}
},
]
}
Now I want to convert it to
var rel_data = [{
"title": "asa",
"Image": "sasa",
"view_node": "sajsjla"
}, {
"title": "asa",
"Image": "sasa",
"view_node": "sajsjla"
}]
And This is my code to convert data to rel_data
data = data.nodes;
for (d in data) {
rel_data[d].title = data[d].node.title;
rel_data[d].image = data[d].node.Image;
}
for (d in rel_data) {
alert(rel_data[d].title);
}
But it does not seem to work, is there anything wrong in my code?
You're trying to edit objects which aren't there yet.
The first thing you do is access rel_data while there is no rel_data yet. The second thing you do is that you change properties of array elements which do not exist.
Your code should be the following to work:
data = data.nodes;
rel_data = new Array(); // Create rel_data
for(d in data) {
rel_data[d] = new Object(); // Create array element
rel_data[d].title= data[d].node.title;
rel_data[d].image= data[d].node.Image;
}
for(d in rel_data) {
alert(rel_data[d].title);
}
You could also use map instead of a for loop (for generating the new array, you'd want one for the alert())
var rel_Data = data.nodes.map(function(item) {
return {
"title": item.node.title,
"Image": item.node.Image,
"view_node": item.node.view_node
};
});
I'm building the JSON object using JavaScript. How would I inset the following data to the bottom of the stack:
"hello": { "label":"Hello", "url":"#hello" }
in to the following variable:
var ListData = {
"main": {
"label":"Main",
"url":"#main"
},
"project": {
"label":"Project",
"url":"#project"
},
"settings": {
"label":"Settings",
"url":"#settings",
"subnav":[
{
"label":"Privacy",
"url":"#privacy"
},
{
"label":"Security",
"url":"#security"
},
{
"label":"Advanced",
"url":"#advanced"
}
]
}
};
So the variable looks like:
var ListData = {
"main": {
"label":"Main",
"url":"#main"
},
"project": {
"label":"Project",
"url":"#project"
},
"settings": {
"label":"Settings",
"url":"#settings",
"subnav":[
{
"label":"Privacy",
"url":"#privacy"
},
{
"label":"Security",
"url":"#security"
},
{
"label":"Advanced",
"url":"#advanced"
}
]
},
"hello": {
"label":"Hello",
"url":"#hello"
}
};
I used the following code but it doesn't seem to work:
var NewData = '"hello": { "label":"Hello", "url":"#hello" }';
ListData.push(NewData);
You can insert it directly with an object literal:
ListData.hello = { label: "Hello", url: "#hello" };
If you are using jQuery, you can use the .extend() jQuery API like:
$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});
I have one more solution using underscore.js module,
var _ = require("underscore");
var src = {
"main": {
"label": "Main",
"url": "#main"
},
"project": {
"label": "Project",
"url": "#project"
},
"settings": {
"label": "Settings",
"url": "#settings",
"subnav": [
{
"label": "Privacy",
"url": "#privacy"
},
{
"label": "Security",
"url": "#security"
},
{
"label": "Advanced",
"url": "#advanced"
}
]
}
};
var dest = {"hello": { "label":"Hello", "url":"#hello" }};
var data = _.extend(src, dest);
console.log(JSON.stringify(data));
Required op :
{"main":{"label":"Main","url":"#main"},"project":{"label":"Project","url":"#project"},"settings":{"label":"Settings","url":"#settings","subnav":[{"label":"Privacy","url":"#privacy"},{"label":"Security","url":"#security"},{"label":"Advanced","url":"#advanced"}]},"hello":{"label":"Hello","url":"#hello"}}
Keeping with you object literal statements just add another object to your ListData object.
ListData.hello = { "label":"Hello", "url":"#hello" };
push is only for Javascript Arrays.
A JavaScript Object Literal is a comma-separated list of name/value pairs wrapped by a pair of curly braces.
To append the property name of encampment name with a value of Valley Forge to the bottom of the stack, simply add the property name after the JSON object with a dot syntax. Then specify the value. (See 'Append data' below)
You can also delete the appended name/value pair from the object literal. (See 'Delete data below')
// Start with some JSON
var myJson = { "name":"George Washington", "rank":"General", "serial":"102" };
// Append data
myJson.encampment = "Valley Forge";
// Delete data
delete myJson.encampment