Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 612

Replace single backslash with double backslash so that json becomes parseable

$
0
0

I am using cURL inside bash to fetch json data from remote API. Also using jq to parse and loop through the json array to perform additional operations. The response looks like this:

[     {"services": ["HOM"        ],"specialServiceId": "APPOINTMENT","brandedName": "Appointment Home Delivery®","incompatibleSpecialServices": ["DATE","EVENING"        ],"inputParameterRules": [            {"name": "PHONE_NUMBER","brandedName": "Appointment Phone Number","required": true,"format":"^\d{10,15}$","description": "A valid 10 to 15 digit phone number including area code and optional extension"            }        ]    },    {"services": ["NDA_AM_EH","2DA"        ],"specialServiceId": "NO_SIG","brandedName": "No Signature Required","categoryId": "DEL_CON","incompatibleSpecialServices": ["SIG","DIRECT_SIG"        ],"inputParameterRules": [            {"name": "SIGNATURE_RELEASE_NUMBER","description": "A valid signature release number"            }        ],"specialServiceLevel": "ALLPKG"    },  {...},  {...}] 

As you can see the format field inside inputParameterRules for PHONE_NUMBER contains a single backslash which makes this JSON un-parseable.

I would like to

  1. Either replace the single backslash with a double backslash before storing this json in a file OR
  2. Delete the format field.

Tried something like this:

 SPECIAL_SERVICES_RESPONSE=$(curl -s -X GET -H "Authorization: Bearer ${BEARER_TOKEN}" "${SPECIAL_SERVICES_URL}" | jq '[.[].inputParameterRules[] | del(.format)]')

But it produces no results.

Thanks


Viewing all articles
Browse latest Browse all 612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>