Invalid JSON data with base64 string on Postman - javascript

I need to use Postman to send some JSON request. I have an issue here with base64 string. How can I format correctly as JSON property? Hope I have done wrong there.
Note: base64 string has more length. I have extracted part of it.
{
"api_key": "m40q-412u-99bd-388d-yazn",
"file_name": "fileName.pdf",
"file_content": "data:image/*;charset=utf-8;base64,JVBERi0xLjQKJe+/ve+/ve+/ve+/vQoxIDAgb2JqCjw8CiAgL1R5cGUgL0NhdGFsb2cKICAvUGFn
ZXMgMiAwIFIKPj4KZW5kb2JqCgoyIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2VzCiAgL01lZGlhQm94
IFsgMCAwIDI0ODAgMzUwOCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2Jq
CgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL01lZGlhQm94IFsg
MCAwIDU5MyA4NDEgXQogIC9SZXNvdXJjZXMgPDwKICAgIC9Gb250IDw8CiAgICAgIC9GMSA0IDAg
UgogICAgPj4KICAgIC9YT2JqZWN0IDw8CiAgICAgIC9pbWcxIDYgMCBSCiAgICA+PgogID4+CiAg
L0NvbnRlbnRzIDUgMCBSCj4+CmVuZG9iagoKNCAwIG9iago8PAogIC9UeXBlIC9Gb250CiAgL1N1
YnR5cGUgL1R5cGUxCiAgL0Jhc2VGb250IC9UaW1lcy1Sb21hbgogIC9FbmNvZGluZyAvV2luQW5z
aUVuY29kaW5nCj4+CmVuZG9iagoKNSAwIG9iago8PAogIC9MZW5ndGggNjIKPj4Kc3RyZWFtCnEK
MSAwIDAgMSAwIDAgY20KMSAwIDAgMSAwIDAgY20KNTkyIDAgMCA4NDEgMCAwIGNtCi9pbWcxIERv
ClEKZW5kc3RyZWFtCmVuZG9iagoKNiAwIG9iago8PAogIC9MZW5ndGggMzQ3NDUKIC9UeXBlIC9Y
T2JqZWN0CiAvU3VidHlwZSAvSW1hZ2UKIC9GaWx0ZXIgL0RDVERlY29kZQogL1dpZHRoIDQyMwog"
}
My API shows this error:
{
"errors": [
{
"code": 400,
"detail": "Invalid JSON data."
}
]
}

The problem seems to be with the new line (carriage return), just copy this json and paste it in jsonlint.com you will get to know.
You can further refer this question for more details Multiline strings in JSON

Related

How to turn a json string into a properly beautified string to be used in email using a Logic App

Logic App
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Body": "<p><br></p>\n<pre><code>Message 1<br>\n<br>\n#{variables('Message1')}<br>\n<br>\nMessage 2<br>\n<br>\n#{variables('Message2')}</code></pre>\n<pre><code><br>\n<br>\n<br>\n<br>\n</code></pre>",
"Importance": "Normal",
"Subject": "Test",
"To": "test#test.com"
}
This is how it looks like in the email. I know the Message 2 has it already nicely formatted but I would want both to look the same.
Any ideas are appreciated.
After reproducing issue from my side, I got the expected results by taking Parse Json Action after initialize variable action.
As shown in below images i have taken initialize variable actions and send email.
With out parse Json action I got below output in email.
As shown in below image in Parse Json action take Content as Message 1 from dynamic content .
Schema :
{
"Request_Date": "2023-07-18",
"Number_of_Adults": "5",
"Number_of_Children": "1",
"Total_Cost": "690"
}
Then in Send Email action in body take your required data from dynamic content of parse Json 1 as shown in below image.
Then the logic App ran successfully and email got received with expected output format.
Reference MS document for parse Json.
replace(replace(replace(variables('Message1'),',',',<br> '),'{','{<br> '),'}','<br>}')

JavaScript: read and write json file

I have generated json file from my consumer pact in javaScript. When it's generating json file it doesn't contain what I expect. So, now I want to add block to my json file which I don't know how to do? Can someone here help me with that? so, basically I want to read json then create block and then write into json and save data
"path": {
"dataType": "String",
"expression": "data/xml/${id}",
"key": "request"
}
Thanks,
As #Barmar pointed out you can simply parse your JSON text and modify it.
let yourJsonText = "{...}"; // read in your json however you want
let yourJson = JSON.parse(yourJsonText);
// then modify your object
yourJson.path.newAttr = "something you like";
// parse it back to a JSON string
let yourUpdatedJsonText = JSON.stringify(yourJson);

Reading JSON data from JS file

I am uploading one JS file using HTML input file tag. I am reading the data in Python. Since in my data var acb_messages is written, I am not able to parse it. And I want to use this variable name to get the data so I can remove it.
var acb_messages = {"messages": [{
"timestamp": 1475565742761,
"datetime": "2016-10-04 12:52:22 GMT+05:30",
"number": "VM-449700",
"id": 1276,
"text": "Some text here",
"mms": false,
"sender": false
}
]}
How can I parse it in Python and then how can I use it?
Two approaches that I would try if I were at your place -
Convert my .js file to .json file and then using method suggested by #Sandeep Lade.
Reading .js file as string, cropping out the value part and then using json.loads(<cropped part>) as suggested by #rahul mr.
Here is how to achieve 2nd solution -
import json
with open('your_js_file.js') as dataFile:
data = dataFile.read()
obj = data[data.find('{') : data.rfind('}')+1]
jsonObj = json.loads(obj)
What's happening here is that you are finding first reading your .js file (that contains js object that needs to be converted into json) as string, find first occurence of { and last occurence of }, crop that part of string, load it as json.
Hope this is what you are looking for.
Warning - Code works only if your js file contains js object only.
The options above are correct, but the JSON syntax in JS can be a little different than in Python:
example.js:
property.docs = {
messages: {
timestamp: 1475565742761,
datetime: "2016-10-04 12:52:22 GMT+05:30",
number: "VM-449700",
id: 1276,
text: "Some text here",
mms: false,
sender: false
}
};
Therefore we need one more tweak that I found at: How to convert raw javascript object to python dictionary?
The complete code should be:
import json
import demjson
with open('example.js') as dataFile:
data = dataFile.read()
json_out = data[data.find('{'): data.rfind('}')+1]
json_decode = demjson.decode(json_out)
print(json_decode)
import json
jsonfile=open("/path/to/json file")
data=json.load(jsonfile)
the above code will store will store your json data in a dictionary called data. You can then process the dictionary

JSON parsing and stringify

I am currently working on a Javascript project where I have to parse tons of data. The project requires that I parse some JSON data and bring specific data into another array. Right now, I am using the JSON.stringify method and I can console.log all of the data that I need. The data looks something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.3865810000125,
33.90171899971196
],
[
-85.38659500025622,
33.9017919996593
],
yes, this is not all that data-it is about 1200 pages long! I only pasted in the top segment of it. All I really need is how to get to the coordinates aspect of it and into an array. So what I am currently doing is this:
var work = JSON.stringify(response, null, 4)
console.log(work);
Which gives me the above response. However, I am not that familiar with JSON.stringify so if I do:
console.log(work.type);
Attempting to see what the value is for type, I get a response of undefined. Now if I try doing this:
var work = JSON.parse(response)
console.log(work);
I get a response of: VM296:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
Thus, I cannot get the data parsed which I want to get to which will be the coordinates data. Any help with using stringify would great help me. I have read a lot about how it turns the data into a string but have not really seen a lot about how to parse it. Do I have to use parse? Thank you for your help!
You dont' have to use stringify or parse for this. The response object you are receiving is already a javascript object so you can reference its properties normally
response.type; // => "FeatureCollection"
If you try and stringify the response it will lose its type property, for instance:
typeof "some string".someProperty; // => undefined

Unexpected token ILLEGAL : how to remove &quote; and lines breaks from JSON

I receive a jsonfrom the server when loading a page, and it gets populated in the page using Handelbars.java. Till now everything's fine.
However I would like to get that jsonobject and store it on a javascriptobject this is where I fail.
When I try to store it on a javascript object I do the following :
var jsObject = "{{output.items}}"; // the output.items is the JSON retrieved on the template
I get the following (&quote; and line breaks interpreted) :
{
"profile": {
"name": "copernic",
"email": "copernic#cop.com"
}
....
}
When I should get the following (without line breaks interpreted) :
{
"profile": {
"name": "copernic",
"email": "copernic#cop.com"
}
....
}
So it throws me an error on javascriptUncaught SyntaxError: Unexpected token ILLEGAL.
When printing the json on the HTML template using <pre>{{output}}</pre> it looks just fine.
Do you have any idea about how can I store the jsonreturned from the server on page loading on a javascriptobject as I don't have much control of it since it's NOT coming with json?
Thank you.
Your server is serving a incorrect json format.
Your server needs to serve a json string, with double quotes.
{
"profile": {
"name": "copernic",
"email": "copernic#cop.com"
}
}
If you need to serve a well-formed json, you can try with Gson.
https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson
Since the content output.items ist a string, this is how you can proceed.
eval("jsObject={" + object.items.replace(/"/g,'"')+"}");

Categories