Reputation: 91
I have a string converted from a JSON-Object, which has multiple escape characters preceding each variable. I found a solution for removing one set of escape chars. how do I clean up and format my json string?
Here's how my string looks like:
{"formFactor":"{\"form_factor_type\":\"SOMETHING\",\"payment_card\":\"{\\"SOMETHING\\":\\"NOTPRESENT\\",\\"card_holder_present\\":\\"NOTPRESENT\\",\\"UUID\\":\\"SOMETHING\\",\\"instrument_id\\":\\"SOMETHING\\",\\"signature_id\\":\\"SOMETHING\\"}\"}","txnCharacteristicsAmt":"[\"{\\"amount\\":\\"{\\\\"amount\\\\":\\\\"00\\\\",\\\\"code\\\\":\\\\"USD\\\\"}\\",\\"type\\":\\"CASH_OVER\\"}\"]","pmtCardInfo":"{\"card_present\":\"PRESENT\",\"card_holder_present\":\"PRESENT\",\"UUID\":\"SOMETHING\",\"instrument_id\":\"SOMETHING\"}","terminalInfo":"{\"cardholder_authentication_capability_used\":\"SIGNATURE\",\"pin_entry_capability\":\"UNKNOWN\",\"operating_environment\":\"ADMINISTRATIVE_TERMINAL\",\"terminal_id\":\"SOMETHING\",\"pan_entry_mode\":\"FULL_MAGNETIC_STRIPE\",\"card_data_entry_capability\":\"UNKNOWN\",\"pos_condition_code\":\"UNSPECIFIED\",\"cardholder_authentication_capability\":\"UNKNOWN\",\"pos_entry_mode\":\"FULL_MAGNETIC_STRIPE\",\"card_data_entry_capability_used\":\"UNKNOWN\"}","is_dyson_present":"SOMETHING","productInfo":"{\"tenant\":\"SOMETHING\",\"integration\":\"SOMETHING\",\"product_name\":\"SOMETHING\",\"tokenized_value_type\":\"SOMETHING\"}","cashOverAmount":"SOMETHING","counterPrtyAccNum":"SOMETHING","merchantDetails":"{\"merchant_address\":\"{\\"state\\":\\"SOMETHING\\",\\"address1\\":\\"SOMETHING\\",\\"city\\":\\"SOMETHING\\",\\"iso_country\\":\\"US\\"}\",\"merchant_category_code\":\"SOMETHING\",\"merchant_name\":\"SOMETHING\",\"issuer_list\":\"[\\"SOMETHING\\"]\",\"external_merchant_id\":\"SOMETHING\",\"acquiring_institution_country_code\":\"US\"}}
Upvotes: 0
Views: 2642
Reputation: 159250
Whoever generated that string really messed up. Badly!!
Each nested object was independently converted to JSON text, and that text was then assigned as a string value, rather than converting the entire object hierarchy as a single JSON structure.
They then encoded the nested strings incorrectly, resulting in bad JSON. Obviously they didn't use a good JSON library.
The correct solution is to fix the code that generated that string.
Below is your string formatted for human readability. The problem is that the root object starts with a formFactor
field. Its value is an object, but rather than having the object as the field value, the object was converted to JSON text, and the formFactor
field has the text as its value. This is repeated for the second field (payment_card
), but the escaping is wrong:
"formFactor":"{
\"form_factor_type\":\"SOMETHING\",
\"payment_card\":\"{
\\"SOMETHING\\":\\"NOTPRESENT\\",
\\"card_holder_present\\":\\"NOTPRESENT\\",
\\"UUID\\":\\"SOMETHING\\",
\\"instrument_id\\":\\"SOMETHING\\",
\\"signature_id\\":\\"SOMETHING\\"
}\"
}"
The double-nested object's JSON text is incorrectly encoded. To see that, let's start from the inside. The payment_card
object is:
{
"SOMETHING": "NOTPRESENT",
"card_holder_present": "NOTPRESENT",
"UUID": "SOMETHING",
"instrument_id": "SOMETHING",
"signature_id": "SOMETHING"
}
When that text is then encoded as a JSON string for the formFactor
object, all the quotes ("
) are escaped:
{
"form_factor_type": "SOMETHING",
"payment_card": "{
\"SOMETHING\": \"NOTPRESENT\",
\"card_holder_present\": \"NOTPRESENT\",
\"UUID\": \"SOMETHING\",
\"instrument_id\": \"SOMETHING\",
\"signature_id\": \"SOMETHING\"
}"
}
When that text is then encoded (again!) as a JSON string for the formFactor
field of the root object, all the backslashes (\
) and quotes ("
) are escaped:
"formFactor": "{
\"form_factor_type\": \"SOMETHING\",
\"payment_card\": \"{
\\\"SOMETHING\\\": \\\"NOTPRESENT\\\",
\\\"card_holder_present\\\": \\\"NOTPRESENT\\\",
\\\"UUID\\\": \\\"SOMETHING\\\",
\\\"instrument_id\\\": \\\"SOMETHING\\\",
\\\"signature_id\\\": \\\"SOMETHING\\\"
}\"
}"
But as you can see, that is not what happened. The number of backslashes (\
) are wrong in your string, i.e. your string is corrupt.
Fix the code that generated the string. Either by correctly escaping the multi-nested strings, or by not independently converting nested objects to JSON text. I'd suggest the second option, i.e. the above should just be:
"formFactor": {
"form_factor_type": "SOMETHING",
"payment_card": {
"SOMETHING": "NOTPRESENT",
"card_holder_present": "NOTPRESENT",
"UUID": "SOMETHING",
"instrument_id": "SOMETHING",
"signature_id": "SOMETHING"
}
}
{
"formFactor":"{
\"form_factor_type\":\"SOMETHING\",
\"payment_card\":\"{
\\"SOMETHING\\":\\"NOTPRESENT\\",
\\"card_holder_present\\":\\"NOTPRESENT\\",
\\"UUID\\":\\"SOMETHING\\",
\\"instrument_id\\":\\"SOMETHING\\",
\\"signature_id\\":\\"SOMETHING\\"
}\"
}",
"txnCharacteristicsAmt":"[
\"{
\\"amount\\":\\"{
\\\\"amount\\\\":\\\\"00\\\\",
\\\\"code\\\\":\\\\"USD\\\\"
}\\",
\\"type\\":\\"CASH_OVER\\"
}\"
]",
"pmtCardInfo":"{
\"card_present\":\"PRESENT\",
\"card_holder_present\":\"PRESENT\",
\"UUID\":\"SOMETHING\",
\"instrument_id\":\"SOMETHING\"
}",
"terminalInfo":"{
\"cardholder_authentication_capability_used\":\"SIGNATURE\",
\"pin_entry_capability\":\"UNKNOWN\",
\"operating_environment\":\"ADMINISTRATIVE_TERMINAL\",
\"terminal_id\":\"SOMETHING\",
\"pan_entry_mode\":\"FULL_MAGNETIC_STRIPE\",
\"card_data_entry_capability\":\"UNKNOWN\",
\"pos_condition_code\":\"UNSPECIFIED\",
\"cardholder_authentication_capability\":\"UNKNOWN\",
\"pos_entry_mode\":\"FULL_MAGNETIC_STRIPE\",
\"card_data_entry_capability_used\":\"UNKNOWN\"
}",
"is_dyson_present":"SOMETHING",
"productInfo":"{
\"tenant\":\"SOMETHING\",
\"integration\":\"SOMETHING\",
\"product_name\":\"SOMETHING\",
\"tokenized_value_type\":\"SOMETHING\"
}",
"cashOverAmount":"SOMETHING",
"counterPrtyAccNum":"SOMETHING",
"merchantDetails":"{
\"merchant_address\":\"{
\\"state\\":\\"SOMETHING\\",
\\"address1\\":\\"SOMETHING\\",
\\"city\\":\\"SOMETHING\\",
\\"iso_country\\":\\"US\\"
}\",
\"merchant_category_code\":\"SOMETHING\",
\"merchant_name\":\"SOMETHING\",
\"issuer_list\":\"[
\\"SOMETHING\\"
]\",
\"external_merchant_id\":\"SOMETHING\",
\"acquiring_institution_country_code\":\"US\"
}
}
Upvotes: 4