I need to transform this:
{"input":{"text":"HI"},"output":{"text":["OK1","TWO"]}}
Into this:
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"time": "1511281401.991815",
"message": "HI",
"from": "me"
},
{
"time": "1511281401.991837",
"message": "OK1"
"from": "bot"
}
{
"time": "1511281401.991847",
"message": "TWO"
"from": "bot"
}
]}]}
Is it possible at all?
Key issue here is that number of "records" in the localDB should vary depending on the number of entries in .output.text node. There could be just one text, or three or more.
I tried with this, but it is not quite working:
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"time" : now|tostring, "message" : .input.text, "from" : "me"},
{"time" : now|tostring, "message" :.output.text, "from" : "bot"}
]
}]
}
I think you are very close. You just need to use .output.text[]
and take advantage of how Object Construction behaves when a member expression returns multiple results. Try this:
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"time" : now|tostring, "message" : .input.text, "from" : "me"},
{"time" : now|tostring, "message" :.output.text[], "from" : "bot"}
]
}]
}
Sample Output
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"time": "1511283566.11608",
"message": "HI",
"from": "me"
},
{
"time": "1511283566.116094",
"message": "OK1",
"from": "bot"
},
{
"time": "1511283566.116094",
"message": "TWO",
"from": "bot"
}
]
}
]
}
Here is a filter which uses a settime
function to assign different times to the rows:
def settime: reduce range(length) as $i (.; .[$i].time = (now + $i|tostring));
{
"localDB" : [{
"tableName": "Default",
"mode": "append",
"data": [
{"message" : .input.text, "from" : "me"},
{"message" :.output.text[], "from" : "bot"}
] | settime
}]
}
Sample Output
{
"localDB": [
{
"tableName": "Default",
"mode": "append",
"data": [
{
"message": "HI",
"from": "me",
"time": "1511285948.684203"
},
{
"message": "OK1",
"from": "bot",
"time": "1511285949.684243"
},
{
"message": "TWO",
"from": "bot",
"time": "1511285950.684261"
}
]
}
]
}