Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | 'use strict'; |
2 | |||||
3 | /** | ||||
4 | * Converts a JSON string to object | ||||
5 | * @param jsonString | ||||
6 | * @param source | ||||
7 | */ | ||||
8 | const jsonStringToObject = (jsonString, source = {} ) => { | ||||
9 | const s = jsonString.replace(/'/g, '"'); | ||||
10 | try { | ||||
11 | return Object.assign(source, JSON.parse(s)); | ||||
12 | } | ||||
13 | catch (e) { | ||||
14 | throw new Error(`Failed to parse json string: ${s}. Error: ${e.message}`); | ||||
15 | } | ||||
16 | }; | ||||
17 | |||||
18 | export { jsonStringToObject }; |