Ask Your Question
0

How can I restructure nested fields to be inside a new parent?

asked 2020-05-08 12:56:27 -0500

jeff gravatar image

How can I change this field structure

{
  "data1": [
    {
      "vlan": "195",
      "vlanname": "Subnet-54.14.195"
    },
    {
      "vlan": "195",
      "vlanname": "Subnet-54.14.193"
    }
  ]
}

to this new structure?

{
  "data1": [
    {
      "vlan": {
        "id": "195",
        "name": "Subnet-54.14.195"
      }
    },
    {
      "vlan": {
        "id": "195",
        "name": "Subnet-54.14.193"
      }
    }
  ]
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-05-08 13:07:13 -0500

jeff gravatar image

updated 2020-05-08 13:52:03 -0500

You can use a sequence of Field Mapper processors to achieve this. It can be done in a series of steps:

  1. Add a new field that will be the new parent of the existing fields
  2. Change its type to MAP
  3. Add an _id suffix to the existing vlan field
  4. Move the fields starting with vlan to the new parent
  5. Rename the new MAP field to be vlan

See sample pipeline (created in Data Collector 3.13), here (download, import as JSON, run preview).

It might be simpler to use a scripting processor, or some other set of processors, to achieve this use case (depending on how many fields need moved around, and how predictable their structure is). See other answers to this question.

edit flag offensive delete link more
1

answered 2020-05-08 13:13:42 -0500

metadaddy gravatar image

I solved this with an Expression Evaluator and Field Remover. I used Dev Raw Data Source to simulate your use case; presumably you are reading the JSON from a file or similar.

image description

  1. Note that you should set 'JSON Content' to 'JSON array of objects' in the origin's 'Data Format' tab.

image description

  1. The output of the origin looks like this:

image description

  1. Configure an Expression Evaluator to restructure the record. Note the use of a temporary field.

image description

  1. The output of the Expression Evaluator looks like this:

image description

  1. Configure a Field Remover to remove everything except /vlan

image description

  1. Now you have exactly what you need

image description

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-05-08 12:56:27 -0500

Seen: 49 times

Last updated: May 08