2
votes

Is there a way to add extra property in array item with top level object value? My input is like

{
  "ABC": {
    "news": [{
        "datetime": "2019-03-06T14:12:00-05:00",
        "source": "source 1",
        "summary": "asd ada dsd f ef sdf vert"
      }
    ]
  },
  "XYZ": {
    "news": [{
        "datetime": "2019-03-06T14:12:00-05:00",
        "source": "source 1",
        "summary": "asd ada dsd f ef sdf vert"
      }
    ]
  }
}

and expected output is

"news": [{
    "symbol": "ABC",
    "datetime": "2019-03-06T14:12:00-05:00",
    "source": "source 1",
    "summary": "asd ada dsd f ef sdf vert"
  },
  {
    "symbol": "XYZ",
    "datetime": "2019-03-06T14:12:00-05:00",
    "source": "source 1",
    "summary": "asd ada dsd f ef sdf vert"
  }
]

by using below spec I could merge two arrays, but I struggling to add symbol in the item.

[{
  "operation": "shift",
  "spec": {
    "*": {
      "news": {
        "*": ""
      }
    }
  }
}]
1

1 Answers

1
votes

I managed to do it using 2 shift rules. First I extract symbol name to an array and merge the news list and on the second i push the symbol element into the news items (using the correct array index position):

 [{
     "operation": "shift",
     "spec": {
       "*": {
         "$": "symbol",
         "news": {
           "*": "news"
         }
       }
     }
}, {
     "operation": "shift",
     "spec": {
       "news": {
         "*": {
           "@(3,symbol[&])": "news[&1].symbol",
           "*": "news[&1].&"
         }
       }
     }
}
 ]

See if this is what you were looking for. Cheers