Forum Discussion
Option 1: If your goal is to ensure that the table is not empty at all times. You can do the following
insert into a temp table. After the insert is successful (add a tail snap to the output view of the insert snap. The tail snap will wait for its input snap to complete before sending the last n documents to its output view)
use execute snap to truncate original table (using execute snap)
use execute snap to insert into original table as select * from temp table (using execute snap)
Option 2: If you are ok with truncating the table just before you start the insert.
use the execute snap to truncate table (enable an output view)
have the rest of the pipeline to read source data and insert into the table
- Aleksandar_A3 years agoContributor III
Hi @darshthakkar,
One approach will be to Map the input document with the following expression:
$.entries().sort((a,b) => a[0].localeCompare(b[0])).reduce((acc,curr) => acc.extend({[curr[0]]:curr[1]}),{})
Note: This will only sort properties on first level.
Let me know if this helps.
BR,
Aleksandar. - darshthakkar3 years agoValued Contributor
Thank you @AleksandarAngelevski for your suggestion.
I will try it out and keep you posted with my findings…- darshthakkar3 years agoValued Contributor
@AleksandarAngelevski - You’re right, Alphabetical sorting doesn’t work on the first level so you’ll have to break that hierarchy and once you’re at the final stage, your expression will definitely work, I’ve tested it and can confirm that it works.
How I broke the hierarchy: Using JSON Splitter to reorganise JSON - #3 by Supratim (thank you @Supratim for your comments on a different thread)
- darshthakkar3 years agoValued Contributor
As we cannot mark multiple suggestions as “Solutions”, wanted to summarize the solution that will work for this use case:
1. $.entries().sort((a,b) => a[0].localeCompare(b[0])).reduce((acc,curr) => acc.extend({[curr[0]]:curr[1]}),{})
2. $.keys().sort().toObject((k,i)=> k, (k,i)=> $.get(k))
Both the expressions mentioned above will ONLY work if we are at the last stage of the hierarchy.
How I broke the hierarchy: Using JSON Splitter to reorganise JSON - #3 by Supratim
Thank you @AleksandarAngelevski , @alchemiz and @Supratim for your inputs. Closing this thread now.
- darshthakkar3 years agoValued Contributor
@AleksandarAngelevski - Is it a safe assumption that we can use the same expression with a minor tweak to organize data alphabetically even if it’s not the JSON format?
- Aleksandar_A3 years agoContributor III
Well, if the data that we have on input is in different format I might consider another approach because this is a special use case.
- darshthakkar3 years agoValued Contributor
Makes sense, thank you @AleksandarAngelevski.
- darshthakkar3 years agoValued Contributor
Hey @alchemiz,
The solution you provided worked, the only consideration is that - “You need to be at the bottom of the hierarchy for sorting to work”.
Thank you, anyways 🙂
Related Content
- 2 years ago
- 3 years ago
- 12 months ago
- 4 years ago