New User ~ JSON

I am working on an API. I have been stuck for a long time, but I am getting a response in JSON.

I want to create a variable to parse the JSON. Here is the code that performs a GET.

////////////////////////////////////////////////////////////////////////////////////////////////
// GET with parameters
////////////////////////////////////////////////////////////////////////////////////////////////

api.GET('/chart/1/encounters', {
	params: {
		departmentid: 1,
		
	}
}).on('done', function(response) {
	var appt = response['appointments'][0]

Here is a bit of the JSON I get back.

'{“encounters”:[{“encountertype”:“VISIT”,“patientstatusid”:3,“stage”:“INTAKE”,“status”:“OPEN”,“appointmentstartdate”:“2021-01-20T10:00:00-05:00”,“appointmentid”:1174575,“patientlocationid”:21,“departmentid”:1,“providerid”:1,“encounterdate”:“01\/20\/2021”,“encountervisitname”:“physical exam”,“patientlocation”:“Waiting Room”,“providerlastname”:“Cartwright”,“encounterid”:40438,“lastupdated”:“09\/30\/2020”,“providerfirstname”:“Camille”,“providerphone”:"(555) 004-0271",“patientstatus”:“Ready For Provider”},{“encountertype”:“VISIT”,“patientstatusid”:4,

One field I need to put into another API is provider name, so I need to have a field that would be prvdrfullname.providerfirstname + prvdrfullname.providerlastname

That data I have from the first API I’m working with.

My JavaScript is moderate & I am also using NodeJS as the sample is in Node.js.

I don’t know if this is the proper forum but I’m really having a hardtime parsing the JSON.

Thank you.

Hello there,

If you are getting JSON as a data response, then I would assume you should be parsing it with either:

const data = response.json();

OR

const data = JSON.parse(response);

Is this even remotely what you are struggling with? Or, are you struggling with something else?

Thanks. I tried both of your statements. Maybe I’m not parsing this correct. I tried to print the providerlastname to see if I’m getting any fields into the data variable.

api.GET(’/chart/1/encounters’, {
params: {
departmentid: 1,

	}
}).on('done', function(response) {
	var appt = response['appointments'][0]
	
	
	//const data = JSON.parse(response);
	const data = response.json();
	
	console.log(data.providerlastname);
	
	
}).on('error', log_error)

Well, you jogged something in my thoughts and I figured it out. There are some code I don’t completely understand yet, but of what I do know I’m able to get the object and the fields, example last name. So I can now create four new variables to include the info I need to send on.

api.GET('/chart/1/encounters', {
		params: {
			departmentid: 1,
			
		}
	}).on('done', function(response) {
		var appt = response['encounters'][2]
		
		console.log(appt.providerlastname)
		
	}).on('error', log_error)

I am glad you have figured it out. Keep at it.

Also, I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Happy coding.