1

I've been trying to understand and master creating json objects with javascript, however I cannot seem to figure out how to (first iterate something, and use that value as my key). My second problem is when I see my json result, it always seems to be nested with a preceding blank item before it. Here is what I mean:

My current portion of code is:

.then((data)=>{
    connection.query("SELECT * FROM amion_onCall", function (err, result, fields){
        const fixed = [];
            let i;
            for (i in result){
                aName = result[i].name;
                aServ = result[i].specialty;
                aShift = result[i].shift;
                aOff = result[i].office;
                aCell = result[i].cell;
                aTag  = result[i].tag;
                var data = {aServ: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}};
                // console.log(data);
                fixed.push(data);
            }
        fs.writeFile('./data/json/newAmion.json', JSON.stringify(fixed), function(err){
            if (err) throw err;
            console.log("Wrote New Amion");
        });
    });
})

The output in my json viewer is:

[
 {
   "aServ": {
      "name": "Dr.John",
      "service": "Cardiology",
      "shift": "7a-7p",
      "office": "123",
      "cell": "123-456-789",
      "tag": "no tags"
  }
}, 

...and so on for my ~150 entries.

Problem #1: I want to move this up one full level. I'm not sure how to do that, or why it starts so deeply nested.

Problem #2: When I iterate aServ, I want the actual value to be output in the beginning of my json. My current code prints "aServ" statically for everyone...I don't want to do that. For example, this is how I am trying to get my json to output:

{
  "Cardiology": {
     "name": "Dr.John",
      "service": "Cardiology",
      "shift": "7a-7p",
      "office": "123",
      "cell": "123-456-789",
      "tag": "no tags"
    }, 
  "Pulmonology": { ...and so on
}

2 Answers 2

1

Answer 1:

What do you mean by "preceding blank item"? Do you mean "aServe"? If yes, because you did it here var data = {aServ: { ... } }.

Also, you have a lot of items. So those are inside array [].

Answer 2:

// Change it to object instead of array
const fixed = {};

// Inside array
if (!Array.isArray(fixed[aServ])) {
  fixed[aServ] = []  
}

fixed[aServ].push({
  name: aName,
  service: aServ,
  shift: aShift,
  office: aOff,
  cell: aCell,
  tag: aTag,
})

No need to push item to array.

Remember, if there are multiple aServ with the same value then it will replace the existing item. That is why you should use an array.

Sign up to request clarification or add additional context in comments.

7 Comments

What I mean is when I open this up in a json program it looks like this : Root > Item[0] > aServ > name, service, shift, office, cell tag....instead I want to eliminate the item[0], and bring it to: Root> Cardiology> name, service, shift.etc. If I were to create it like: var data = {name: aName, service: aService...etc} i would still get a preceding blank before in my json like: (item[0]: name, service, cell..etc} and I dont want to have item[0]
Updated the answer.
got it! thank you!! I seem to be running into a new error when changing it to an object: the push function doesnt seem to work anymore. How would I place all of the iterations into the object?
One last question. It makes sense to use an array rather than an object. But there is no actual way to have it where I can name the array items? My current code outputs: item[0], item[1], item[2]...etc but I want it to list "cardiology, pulmonology, etc" as an array with their nested information.
Update the code. Something like that should work. Did not test the code. But you should understand what I am trying to do. I will appreciate an upvote.
|
1

Problem #1: I want to move this up one full level. I'm not sure how to do that, or why it starts so deeply nested.

It's nested because you're in an array of objects. Arrays in JavaScript are indicated by the surrounding brackets []. In this case I think it's what you want: you're creating an array of objects.

Problem #2: When I iterate aServ, I want the actual value to be output in the beginning of my json.

This line:

 var data = {aServ: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}}

Needs to be changed to this:

 var data = {[aServ]: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}}

Note the brackets. That will interpolate your variable values as an array key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.