Skip to content Skip to sidebar Skip to footer

Nested Replace Of Strings With Double Quotes In Javascript

In Logic Apps I have this function which replaces double double quotes with a zero for some strings. How can I replicate this in Javascript? replace(replace(replace(replace(replace

Solution 1:

If you want to use JavaScript to manage the json, just set the value directly. You could refer to my actions as the below pic show. The expression would be like json.key=value;

enter image description here

Update: I test with the array json it could work, you need to point the index. If not please provide more information.

enter image description here

Solution 2:

This treats the json as a string and not an array.

json=json.replace(/"PR_USER8":""/g,'"PR_USER8":0').replace(/"PR_RENT":""/g,'"PR_RENT":0').replace(/"PR_ID":""/g,'"PR_ID":0').replace(/"PR_USUM":""/g,'"PR_USUM":0').replace(/"PR_LEAS":""/g,'"PR_LEAS":0');
return json;```

I think thisis better as it treats the json as an object not a string.

    ```
    var jsonString = workflowContext.actions.HTTP_2.outputs.body;
const json =JSON.parse(jsonString);
const ret =[];

const dic = json['Payload'];
const checkFields = ['PR_RENT','PR_ID','PR_USUM','PR_LEAS','PR_USER8'];
for(i in json['Payload'])
{


checkFields.forEach(field=>{
     if(json.Payload[i][field]=="")
    {
        json.Payload[i][field]=0;
       // console.log(field);
    }
}

);
}

return json;
    ```

Post a Comment for "Nested Replace Of Strings With Double Quotes In Javascript"