Json helper6/25/2023 S/\" *, *\"/,/g replaces single commas, (which may be preceded with zero or more spaces, and/or followed by zero or more spaces) with a single comma. the last occurrence) from the end of the string. S/ *\" *\\] *$// removes the closing square bracket ], which may be preceded or followed by zero or more space characters, and the preceding double quote (i.e. ![]() the first occurrence) from the beginning of the string. S/^ *\\[ *\"// removes the opening square bracket [, which may be preceded or followed by zero or more space characters, and the following double quote (i.e. Let's breakdown each s command to further understand what is happening: The syntax of the s command is 's/regexp/replacement/flags'. The one line of text is then piped to sed's s command for further processing (i.e. Note: A JSON Array can contain newlines between each item and still be valid, so although the example JSON given in your question appears on one line - it is not a requirement of this solution as it will handle multi-line too. This ensures the contents of foobar.json is transformed to one line. This translation will replace any newline characters which may exists in the contents of the source. The content of foobar.json is redirected, (note the < before the filepath), to tr (i.e. This path currently assumes the file is named foobar.json and resides in your Desktop folder (You'll need to change this path to wherever your actual file exists). json file via the part which reads ~/Desktop/foobar.json. On line 3 a shell script is executed via the do shell script command, which performs the following: On line 2 AppleScript's text item delimiters are set to a comma - this will help when extracting each individual value from the source JSON Array and assigning each value to a new AppleScript list. On line 1 AppleScript's current text item delimiters are assigned to a variable named TID. For instance ~/Desktop/foo\\ bar.json Explanation: json filename includes a space(s) you'll need to escape them with \\. ~/Desktop/foobar.json) as necessary.Īlso, if your. Note: you'll need to change the path part (i.e. Set AppleScript's text item delimiters to TID Set myList to text items of (do shell script "tr ''\\ Set AppleScript's text item delimiters to "," This assumes that your JSON file is valid and contains a single Array only: set TID to AppleScript's text item delimiters The resultant AppleScript list is assigned to a variable named myList.īy utilizing tools built-in to macOS as standard, you can also do the following via AppleScript. This assigns each item of the source JSON Array to a new AppleScript list. On line 2 we parse the contents using the JSON Helper plug-in. Note You'll need to change the path part (i.e. ![]() json file and assign it to the variable named srcJson. Tell application "JSON Helper" to set myList to read JSON from srcJson Then use it in your AppleScript as follows: set srcJson to read POSIX file (POSIX path of (path to home folder)
0 Comments
Leave a Reply. |