Solved

NodeJS JSON array to Json obj

Posted on 2015-01-08
3
269 Views
Last Modified: 2015-01-09
I am a NodeJS Javascript newbie

Using nodeJS  I have this JSON array that i read in as a recordset from a database.

[{"CaseType":"110","ServiceType":"12","Description":"Traffic Ticket","Reimbursment":60,"CTCategory":"SCF"}]

I need it to be a JSON object
{"CaseType":"110","ServiceType":"12","Description":"Traffic Ticket","Reimbursment":60,"Category":"TFT"}

So I can do this

var obj = JSON.parse(s);

console.log(obj.CaseType);
console.log(obj.Description);
....
console.log(obj.Category);
0
Comment
Question by:charlesbaldo
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40539533
You can parse an array and then just access as obj[0].CaseType for the first element and so on
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40539597
example:

var rtn = "[{\"CaseType\":\"110\",\"ServiceType\":\"12\",\"Description\":\"Traffic Ticket\",\"Reimbursment\":60,\"CTCategory\":\"SCF\"}]";

var obj = JSON.parse(rtn);

console.dir(obj);

Open in new window


outputs

[[object Object] {
  CaseType: "110",
  CTCategory: "SCF",
  Description: "Traffic Ticket",
  Reimbursment: 60,
  ServiceType: "12"
}]

Open in new window

0
 

Author Closing Comment

by:charlesbaldo
ID: 40540138
Thanks Rob,
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now