Forum Discussion
You can use the mapValues() method to iterate over the values of the object and perform the transformation. The result of that operation is still an object, so you’ll need to use the jsonPath() function with a path of ‘$*’ to get the values of the object into an array. So, the following should do what you want to create the “custom_fields” property:
jsonPath($.mapValues((value, key) => { value: value, id: parseInt(key.match(/\d+/)[0]) }), "$*")
Thanks for looking into it. One followup question. When I tried this, it is double quoting even numbers :
Any help on this?
- tstack8 years agoFormer Employee
In the arrow function, you’ll probably want to use parseFloat() to attempt to convert the string to a number. If parsing the string fails, a NaN will be returned, so you can use a logical-or operator to fallback to the original value. Here it is in code:
parseFloat(value) || value
Here is the updated expression:
jsonPath($.mapValues((value, key) => { value: parseFloat(value) || value, id: parseInt(key.match(/\d+/)[0]) }), "$*")
- ddangler8 years agoNew Contributor III
I appreciate the prompt response and it worked. You saved me again 🙂