Parsing value from nested json and create a new nested JSON

Hello guys,

First of all, thanks for this awesome tool.
Sorry if my questions seems dumb, but I’m kinda new to this.

I have a nested json data that I receive from a webhook in the following format :

{
 headers{
        "value1":"somevalue",
        "value2":"othervalue",
   }
   body{
        "id":"xxxxyy",
        "severity":"3"
        "tags":["tag1","tag2","tag3'],
   }
}

I just need to keep the body data on my side.

From this JSON, I need to create a new nested JSON in order to send a webhook to Microsoft Teams using the following structure :

{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "Summary Goes Here",
   "sections": [
     {
       "activityTitle": "Title",
       "activityImage": "some link to an image",
       "activityImageType": "article",
       "activitySubtitle": "Subtitle",
       "facts": [
         {
           "name": "Severity",
           "value": "High"
         },
         {
           "name": "Tags",
           "value": "tag1 tag2 tag3"
         }
       ],
       "markdown": true
     }
   ],
   "potentialAction": [
     {
       "@context": "http://schema.org",
       "@type": "ViewAction",
       "name": "action name",
       "target": [
         "some http link"
       ]
     }
   ]
 } 

I don’t really know where to begin, is it better to use a function to extract the data and then create the new json or is it better to use a “set” node to extract the data and populate the new json ?

Welcome to the community @jackbot!

It seems like you have some more complex mapping requirements as the data has to get partly transformed (in the source it seems to be a string-number and in the target a string-text). So best to do it via a Function-Node.
No idea how the mapping should work exactly but here an example:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n\t{\n\t\tjson: {\n\t\t\theaders: {\n\t\t\t\t\"value1\": \"somevalue\",\n\t\t\t\t\"value2\": \"othervalue\"\n\t\t\t},\n\t\t\tbody: {\n\t\t\t\t\"id\": \"xxxxyy\",\n\t\t\t\t\"severity\": \"3\",\n\t\t\t\t\"tags\": [\"tag1\", \"tag2\", \"tag3\"]\n\t\t\t}\n\t\t}\n\t}\n];\n"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "const severityLevels = [\n  \"none\",\n  \"Low\",\n  \"Medium\",\n  \"High\"\n]\n\nreturn items.map(item => {\n  return {\n    json: {\n\t\t\"@type\": \"MessageCard\",\n\t\t\"@context\": \"http://schema.org/extensions\",\n\t\t\"themeColor\": \"0076D7\",\n\t\t\"summary\": \"Summary Goes Here\",\n\t\t\"sections\": [\n\t\t\t{\n\t\t\t\t\"activityTitle\": \"Title\",\n\t\t\t\t\"activityImage\": \"some link to an image\",\n\t\t\t\t\"activityImageType\": \"article\",\n\t\t\t\t\"activitySubtitle\": \"Subtitle\",\n\t\t\t\t\"facts\": [\n\t\t\t\t\t{\n\t\t\t\t\t\t\"name\": \"Severity\",\n\t\t\t\t\t\t\"value\": severityLevels[$json.body.severity]\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t\"name\": \"Tags\",\n\t\t\t\t\t\t\"value\": $json.body.tags.join(' ')\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\t\"markdown\": true\n\t\t\t}\n\t\t],\n\t\t\"potentialAction\": [\n\t\t\t{\n\t\t\t\t\"@context\": \"http://schema.org\",\n\t\t\t\t\"@type\": \"ViewAction\",\n\t\t\t\t\"name\": \"action name\",\n\t\t\t\t\"target\": [\n\t\t\t\t\t\"some http link\"\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t}\n  }\n});"
      },
      "name": "Build Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        550
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={{$json}}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        550
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Build Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Data": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Thank you Jan for the quick reply, I’ll take a look at it :slight_smile:
I’ll update the post asap.