Parsing json with dynamic keys

Describe the issue/error/question

I’m having trouble parsing the following json
[
{
“item”: {
“116”: {
“id”: “116”,
“action”: “Updated”,
“lastChanged”: “2023-01-04T21:46:35+00:00”
},
“117”: {
“id”: “117”,
“action”: “Updated”,
“lastChanged”: “2023-01-03T19:00:42+00:00”
}
}
}
]
I need to return a list of the id’s or the keys 116, 117 as they are the same as the id’s

What is the error message (if any)?

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

I can’t share the workflow as it has data I can not share

Share the output returned by the last node

[
  {
    "item": {
      "116": {
        "id": "116",
        "action": "Updated",
        "lastChanged": "2023-01-04T21:46:35+00:00"
      },
      "117": {
        "id": "117",
        "action": "Updated",
        "lastChanged": "2023-01-03T19:00:42+00:00"
      }
    }
  }
]

I’ve tried a list and making my own code but have not been successful as the keys 117, 118 are dynamic so I can’t select them in the item list
my code for the function is not working I tried writing it similar to how I had it in Python but that didn’t work so I know I need to iterate through the item but that is where I am getting confused.

myarr = []
for (const item of $input.all()) {
  

     myarr.push({'empid': 
               item.json});
  
  
   
}
return myarr;

Information on your n8n setup

  • n8n version: 1.8.0
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: desktop app

Hi @dragonfly23

Welcome to the community.
Have a look at the Object Keys function.

@BramKn Thank you!! I got it to work
Using the json file

[
  {
    "body": {
      "latest": "2023-01-06T00:56:56+00:00",
      "employees": {
        "116": {
          "id": "116",
          "action": "Updated",
          "lastChanged": "2023-01-04T21:46:35+00:00"
        },
        "117": {
          "id": "117",
          "action": "Updated",
          "lastChanged": "2023-01-03T19:00:42+00:00"
        }
      }
    }
  }
]

The code

myarr = []
eids1 = $input.all()[0].json.body.employees
eids = Object.keys(eids1)

for (let index = 0; index < eids.length; index++) {
   myarr.push({'eids':eids[index]})
}

return myarr

The results

eids
116	
117
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.