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);
asked Jun 16, 2018 at 20:28
mm1975
1,6555 gold badges32 silver badges54 bronze badges
-
your Jsons are not formatted correctly.amrender singh– amrender singh2018年06月16日 20:31:48 +00:00Commented Jun 16, 2018 at 20:31
-
If it is a string, why would you stringify it?!Jonas Wilms– Jonas Wilms2018年06月16日 20:34:15 +00:00Commented Jun 16, 2018 at 20:34
-
I think he meant to JSON.parseAttersson– Attersson2018年06月16日 20:34:33 +00:00Commented Jun 16, 2018 at 20:34
1 Answer 1
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)
answered Jun 16, 2018 at 20:33
Attersson
4,8841 gold badge19 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js