Obtain data from JSON object with NODE JS - node.js

I have a response from a webservice like this:
[{"record_id":"63","date":"2021-04-12","acept":"1","name":"John","document":"1","passport":"","phone":"999999999","sign":"[document]","activity":"2"}]
There is a var called response that stores that response.
How do I get the "name" and the "phone" from this?
I tried to do JSON.stringify(response) in order to get somehow the info but I don't know what to do next.
Is the response a JSON or just a String??
Should I do JSON.stringify or JSON.parse to be able to work with this?
Thanks a lot

To check to see whether this is a JSON object or string, run console.log(typeof response). If it logs object, then this is already a JSON object! You don't have to do anything, and can get attributes out of it like any other object. (For instance, to get the name attribute, you can run response[0]["name"].) If it logs string, then you'll have to run JSON.parse(response) and save that to a variable to parse the string and turn it into an object.

After parse the response, you can access to name and phone like this:
response[0]["name"]
response[0]["phone"]

Related

What is this structure coming from a fetch that returns a octet-stream?

I'm tring to retrieve a list of files from an API using axios, in Node.js. What I get is what I assume is an ArrayBuffer string. I'm using the following code:
axios.get('http://api.com/filesList').then(response => {
console.log(response.data);
});
This is what I get (changed numbers and letters a bit to hide potential priv info):
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 �����0*$97cceb282¶2022-11-30.csv"�☺
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 ���0*$97cceb282¶2022-11-26.csv"�☺
$97cceb28↕$sad28sdj-askd-3294-3jh9→--Account_123456789 �໑�0*$97cceb282¶2022-11-19.csv"�☺
I know for a fact that I'm supposed to receive certain information for each file of this list, at least which account is associated to them and their name. I did a little digging and added responseType: 'arraybuffer' to the function and then it output a <Buffer 22 be 01...> kind of response. I then tried a method to convert it to a string but I end up getting the result I had without setting the response type.
So, what kind of string data is this and how do I convert it to an object/JSON? Is there a way to decode this structure?
Thank you all in advance.

Body is not getting parsed in GET method

I am using mockoon for API simulation. I created 2 routes there with method GET and its body contains(responds with) JSON object. I noticed that my express app is not able to parse one of the routes. But the route that has JSON object in body which contains ARRAY is getting parsed. I tested both routes with Express(by console.log) and in chrome browser(I have JSON formatter extension) and it is behaving the same meaning response that does not contain ARRAY is not getting parsed but the response with array is getting parsed(behaving normally). Let me show the screenshots:
Express(by console.log):
With array:
Without array:
Chrome(JSON Formatter extension):
With array(extension is able to parse):
Without array(extension is not able to parse):
I tried adding Header(Content-Type: application/json) to the route in mockoon. But still, I am not aware of what is going on here. Someone please explain
The express code:
const iabs_client = await axios.get(
"http://localhost:3001/iabs-client
);
Here is the route created in Mockoon(without array inside JSON):
P.S mockoon is a program that creates endpoints in localhost, useful for API simulation when developing front-end without having backend yet
The trailing comma after "something" is not valid JSON. Edit your Mockoon body to remove the comma and it should work.

Nodejs http server json parse

why the parameter data different in console and website
i need use JSON.stringify or JSON.parse in res.end?
The JSON.stringify() method converts a JavaScript object or value to a JSON string, so add this to your res.end()
res.end(JSON.stringify(data));
I think this code will help you.
res.end(data.toString())
I'll explain the reason why it is different in console and website.
In console, the data is type of buffer, so it can probably output data in byte format.
But in the web browser, the data is parsed in JSON string, so it'll be converted to string and correct result comes out.

NetSuite SuiteScript 2.0 How to parse content Text in suitescript

i am trying to update a vendor record status field using suitescript 2.0,passing the body in postman tool and is working fine for content JSON but the problem is when i try content as Text its getting error don't know how to read body value in suitescript 2.0.
input body from postman
sample code is
function doPut(context)
{
var obj=JSON.stringify(context.ids);-----here is the error context is empty
// tried JSON.parse also getting undefined
log.debug('str: '+obj);
return obj;
}
If you could provide the exact error message that would be helpful.
But in the mean time a few things that you should verify in your script, if you do not pass application/JSON in header, and your data is object, you need to explicitly parse it into JSON(i.e use JSON.parse() on the request-body), and your response type too should be in the same format i.e your response type should match content-type in the request.
looks like you may have the wrong Content-type. Should be Application-json. If not try Json.parse on the body if you're using text/plain. First step is to always log the context to console or run Object.keys(context) to see what's there. Also make sure doPut is exported as a function

How to parse interaction messages coming from Slack?

I want to parse interaction message requests coming from Slack. This is what Slack says in their docs:
The body of that request will contain a payload parameter. Your app
should parse this payload parameter as JSON.
That seemed straightforward, so I parsed it like so:
JSON.parse(decodeURIComponent(body.split('=')[1]))
However, in the string-fields of the resulting object, I see pluses instead of spaces:
"There+should+not+be+pluses+here"
What am I doing wrong here?
Took a look at their library here, and it turns out, they use node's querystring.parse().
So the parsing procedure should look like this:
JSON.parse(querystring.parse(body).payload)

Resources