0

How can i add "name_3" to this json?

    json = {
        "name_1": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ],
        "name_2": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ]
    }

var name_3 = {"kind": "audio","artistId": 4691754},{"kind": "video","artistId": 139918600}

i want to access it like this json.name_3[0]artistId Later, i will have to check if it already exist and rewrite it, or delete it from a custom function

2
  • "name_1": [ ... and "name_2": [ ... , but "name_3": { ... Apples and oranges Commented Feb 9, 2020 at 19:19
  • 1
    I created a node module github.com/onigetoc/virtual-storage to make i virtual storage when you do not have access cookies, LocalStorage and a bd. I did it for my Dialogflow skill. Commented Feb 12, 2020 at 19:07

4 Answers 4

1

Use brackets json["name_3"] = or with a variable json[name] = or dot notation json.name_3 =

const json = {
  "name_1": [{
      "kind": "podcast",
      "artistId": 139918600
    },
    {
      "kind": "podcast",
      "artistId": 139918600
    }
  ],
  "name_2": [{
      "kind": "podcast",
      "artistId": 139918600
    },
    {
      "kind": "podcast",
      "artistId": 139918600
    }
  ]
};


const name = "name_3";
json[name] = [{
  "kind": "movie",
  "artistId": 123456780
}];
console.log(json[name][0])

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

Comments

1
let json = {
    "name_1": [{
        "kind": "podcast",
        "artistId": 139918600
    },
    {
        "kind": "podcast",
        "artistId": 139918600
    }
    ],
    "name_2": [{
        "kind": "podcast",
        "artistId": 139918600
    },
    {
        "kind": "podcast",
        "artistId": 139918600
    }
    ]
}

let name_3 = [{ "kind": "audio", "artistId": 4691754 }, { "kind": "video", "artistId": 139918600 }];

json["name_3"] = name_3;
//or
//json.name_3 = name_3;

//access it:
console.log(json.name_3 && json.name_3[0].artistId);

//check if it's there:
if (json.name_3) console.log("It's there");
else console.log("Not there")

//delete it:
delete json.name_3;

2 Comments

Great thank you, i was not far but my problem is my object was not in bracket [] to begin with.
Yes, that was the problem actually.
0

You can try spread operator to achieve this.

var json = {
        "name_1": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ],
        "name_2": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ]
    };
    
var name_3 = [{"kind": "audio","artistId": 4691754},{"kind": "video","artistId": 139918600}];

console.log({...json, name_3});

Comments

-1

Your json has to be string not object, then you can parse it to object via JSON.parse() method. Then you can add your new element to object and make it json format again using JSON.stringify() method.

const json = `{
        "name_1": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ],
        "name_2": [{
                "kind": "podcast",
                "artistId": 139918600
            },
            {
                "kind": "podcast",
                "artistId": 139918600
            }
        ]
    }`
    const name3 = [{kind: "audio",artistId: 4691754},{kind: "video",artistId: 139918600}];
    let obj = JSON.parse(json);
    Object.assign(obj, name3);
    const newJson = JSON.stringify(obj)

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.