cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to fail a Snap for Invalid json as i/p doc(Duplicate keys)

KumarR
New Contributor

Hi Team,
I am sending a invalid json payload from the postman to my pipeline.
The pipeline has first snap as Copy, when i checked the output of copy it has removed duplicated keys from the invalid json payload. My expectation was that it will throw error as i have sent invalid json.
Any thought on how can i fail the pipeline if the payload coming as invalid json into pipeline/snap?
Below was payload i sent to pipe from postman tool-
{
โ€œwwwโ€: {
โ€œstationโ€: โ€œ3333โ€,
โ€œstationโ€: โ€œ22222โ€,
โ€œstationโ€: โ€œ44444โ€,
โ€œddโ€: 24224,
โ€œdd1โ€: โ€œ34224โ€,
โ€œdd2โ€: โ€œsfsfโ€,
โ€œdd3โ€: โ€œdddsfsfโ€
}
}

The output of copy snap came as below(i wannt the snap to be failed as it was invalid json data)-
{
โ€œwwwโ€: {
โ€œstationโ€: โ€œ44444โ€,
โ€œscaleIdโ€: โ€œwrwrโ€,
โ€œddโ€: 24224,
โ€œdd1โ€: โ€œ34224โ€,
โ€œdd2โ€: โ€œsfsfโ€,
โ€œdd3โ€: โ€œdddsfsfโ€
}
}

Thanks,
Kumar R

7 REPLIES 7

anubhav_nautiya
Contributor

why are you using a copy snap to validate JSON? Copy will just simply give you multiple outputs, feed you input from postman to a JSON generator and it should fail validating the JSON.

Hi Anubhav,
Thanks for looking in this! I gave example of copy snap to show that it removes dup keys in invalid json.
As per your suggestion i tried using Json generator by having i/p view and template to snap as below-

[
{
โ€œwwwโ€ : $www
}
]
But Json generator also has removed dup key and provided output. I want this to be failed as i supplied invalid json from psotman. $www has dup key inside it as โ€˜stationโ€™

Hi,
Can you try JSON.parse() function in a mapper and give it a try.

KumarR
New Contributor

The i/p feed itself was json so canโ€™t use JSON.parse.
To use parse need to make it string first i.e JSON.parse(JSON.stringify($))

However JSON.stringify($) itself removing duplicate key.