Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add key and values from one object into each object in an array via jolt

I’m using jolt and I have an input object where I would like to take the keys out of one property and insert them into each object of an array in another property:

My input:

{
  "jolt_marketplaceMetadata": {
    "NAN_KEY": 1,
    "TEMP": 3
  },
  "jolt_attributes": [
    {
      "name": "HELLO",
      "yyup": 3
    },
    {
      "huh": "please",
      "work": 2
    }
  ]
}

The result I’m aiming for:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 "jolt_attributes": [
    {
      "name": "HELLO",
      "yyup": 3,
      "NAN_KEY": 1,
      "TEMP": 3
    },
    {
      "huh": "please",
      "work": 2,
      "NAN_KEY": 1,
      "TEMP": 3
    }
  ]

I’ve tried a couple of different spec arrays and I’ve gotten pretty close, but I can’t seem to figure out how to get the values in without merging all of the data from each of the objects in the array:

[
  {
    "operation": "shift",
    "spec": {
      "jolt_attributes": {
        "*": {
          "*": "jolt_attributes[&1].&",
          "@(2,jolt_marketplaceMetadata)": "jolt_attributes[&1]"
        }
      }
    }
  }
]

which gives me:

{
  "jolt_attributes": [
    {
      "NAN_KEY": 1,
      "TEMP": 3,
      "name": "HELLO",
      "yyup": 3,
      "huh": "please",
      "work": 2
    },
    {
      "NAN_KEY": 1,
      "TEMP": 3,
      "name": "HELLO",
      "yyup": 3,
      "huh": "please",
      "work": 2
    }
  ]
}

It adds the keys from jolt_marketplaceMetadata, but it also merges all of the key/values from each of the array objects together each time.

Any ideas??

>Solution :

You can use the following shift transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "jolt_attributes": {
        "*": {
          "*": "&2[#2].&",
          "@2,jolt_marketplaceMetadata.NAN_KEY": "&2[#2].NAN_KEY",
          "@2,jolt_marketplaceMetadata.TEMP": "&2[#2].TEMP"
        }
      }
    }
  }
]

where

  • &2 replicates the key of the array ("jolt_attributes")
  • [#2] loops through the indexes of that array after traversing the
    tree 2 levels(once for colon, once for opening curly brace) and generates arraywise(array of objects here) result
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading