0

I have a simple input json object that I would like to turn into an array of json objects, with the number of objects being equal to the number of attributes and each object inheriting one specific attribute. My input looks as follows:

{
  "id": "00abc",
  "attribute1": 100,
  "attribute2": "yes",
  "attribute3": false,
  "attribute4": "up"
}

My desired output would look like this:

{
  "array" : [ {
    "id" : "00abc",
    "repeatedId" : "00abc"    
  }, {
    "attribute1" : 100,
    "repeatedId" : "00abc"
  }, {
    "attribute2" : "yes",
    "repeatedId" : "00abc"
  }, {
    "attribute3" : false,
    "repeatedId" : "00abc"
  }, {
    "attribute4" : "up",
    "repeatedId" : "00abc"
  } ]
}

I have managed to almost get there using the following spec (I want to keep it dynamic as the names of the keys and their number may change at any given time):

[
  {
    "operation": "shift",
    "spec": {
      "#array": {
        "*": {
          "@(2,id)": "&2[#2].repeatedId",
          "@2": {
            "*": "&3.[#2].&"
          }
        }
      }
    }
    }
]

Unfortunately, the spec always produces arrays of the values for every key, i.e. this is the output I'm getting:

{
  "array" : [ {
    "repeatedId" : "00abc",
    "id" : [ "00abc", "00abc", "00abc", "00abc", "00abc" ]
  }, {
    "attribute1" : [ 100, 100, 100, 100, 100 ],
    "repeatedId" : "00abc"
  }, {
    "attribute2" : [ "yes", "yes", "yes", "yes", "yes" ],
    "repeatedId" : "00abc"
  }, {
    "attribute3" : [ false, false, false, false, false ],
    "repeatedId" : "00abc"
  }, {
    "attribute4" : [ "up", "up", "up", "up", "up" ],
    "repeatedId" : "00abc"
  } ]
}

I am unsure how to simply get the value instead of an array on the RHS. Any help is appreciated!

Edit: as you can see inheriting the attribute works fine, I should note.

1 Answer 1

0

Okay it appears I have managed to get the desired output with the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "#array": {
        "*": {
          "@(2,id)": "&2[#2].repeatedId",
          "@": "&2[#2].&"
        }
      }
    }
  }
]

This produces:

{
  "array" : [ {
    "id" : "00abc",
    "repeatedId" : "00abc"
  }, {
    "attribute1" : 100,
    "repeatedId" : "00abc"
  }, {
    "attribute2" : "yes",
    "repeatedId" : "00abc"
  }, {
    "attribute3" : false,
    "repeatedId" : "00abc"
  }, {
    "attribute4" : "up",
    "repeatedId" : "00abc"
  } ]
}

I guess I was overthinking this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.