3

I'm trying to read a simple JSON data, but there's a dot at the key. This is what I'm trying:

{
  "mautic.lead_post_save_update": {
    "mautic.lead_post_save_update": [
      {
        "lead": {
          "isPublished": true,
          "dateAdded": "2017-06-19T09:31:18+00:00",
          "dateModified": "2017-06-19T09:32:24+00:00",
          "createdBy": 1,
          "createdByUser": "John Doe",
          "modifiedBy": 1,
          "modifiedByUser": "John Doe",
          "id": 52,
          "points": 0,
          "color": null,
          "fields": {
            "core": {
              "title": {
                "id": "1",

My problem is the key "mautic.lead_post_save_update"

I need to read like this:

var item = item.mautic.lead_post_save_update.mautic.lead_post_save_update[0].lead.points

If I use: "mautic.lead_post_save_update" it reads as a string...

If I use: mautic.lead_post_save_update the code try to find a subnode...

I dont even know how to research for this...

Wish I made myself clear.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • How about - A. Read the json as string. B. Replace dots with underscores C. Parse the json as you normally do ? – Sukhi Jan 17 '20 at 18:15
  • 2
    How about `item['mautic.lead_post_save_update']['mautic.lead_post_save_update'][0].lead.points` – MarkoCen Jan 17 '20 at 18:25
  • 1
    Thank you very much guys! just use this: item.body["mautic.lead_points_change"]["mautic.lead_post_save_update"][0].contact.points; Works perfectly! thank you all. – Siméia Avellar Pedroso Jan 17 '20 at 18:42

3 Answers3

3

Since your JSON keys have dots, you need to use a different syntax:

var item = item["mautic.lead_post_save_update"]["mautic.lead_post_save_update"][0].lead.points;

Otherwise as you noted Javascript thinks you're describing a hierarchy with the dots.

spork
  • 1,185
  • 1
  • 11
  • 17
1

Try this:

const obj = { "mautic.lead_post_save_update": {"mautic.lead_post_save_update": 12} };
const data = obj["mautic.lead_post_save_update"]["mautic.lead_post_save_update"]; // returns 12
TwistedOwl
  • 1,195
  • 1
  • 17
  • 30
0

In JavaScript property accessors provide access to an object's properties by using the dot notation or the bracket notation, e.g.

let obj = {
  "mautic.lead_post_save_update": {
    "mautic.lead_post_save_update": [{
      "lead": {
        "isPublished": true,
        "dateAdded": "2017-06-19T09:31:18+00:00",
        "dateModified": "2017-06-19T09:32:24+00:00",
        "createdBy": 1,
        "createdByUser": "John Doe",
        "modifiedBy": 1,
        "modifiedByUser": "John Doe",
        "id": 52,
        "points": 0,
        "color": null,
        "fields": {
          "core": {
            "title": {
              "id": "1"
            }
          }
        }
      }
    }]
  }
};
console.log(obj["mautic.lead_post_save_update"]["mautic.lead_post_save_update"][0].lead.points);
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34