20295, "Ethan-DeBandi99", "JSON string to Map/List", "2022-07-25T12:26:29Z"
opened 12:26PM - 25 Jul 22 UTC
### Summary
<!--
I have run into a few situations where it would be extremely … useful to take a JSON string and load it into a `Map` or a `List` depending on the format. I mainly need the ability to create `map(string, string)` and `list(string)`.
I found the `map.readThis()` functionality and it appears to be exactly what I am looking for. However, when attempting to use it, I get various JSON formatting errors. Maybe I had something wrong for the map, but I am not sure because I can read the same format into a record with properties matching the JSON keys. The format that I need to read in is `"{\"key1\":\"val1\", \"key2\":\"val2\", ..., \"keyN\":\"valN\"}.
Again, I looked at using `list.readThis()`, but experienced similar errors related to formatting of the JSON. The format I am loading is `"[\"str1\", \"str2\", ..., \"strN\"]"`.
I also found issue [#18156](https://github.com/chapel-lang/chapel/issues/18156), which seems to indicate that the objects cannot be read into these formats, which is why I am not submitting this as a bug.
-->
### Steps to Reproduce
**Source Code:**
Please note: Single quotes used here, but have also tried with `\"`. Is this correct? It works when reading into a record.
```chapel
var json_str = "{'key1': 'val1', 'key2': 'val2'}";
var newmem = openmem();
newmem.writer().write(json_str);
var nreader = newmem.reader();
var m: map(keyType=string, valType=string);
m.readThis(nreader);
```
**Produces This Error**
```
uncaught BadFormatError: bad format: missing expected literal (while reading ioLiteral "}" with path "unknown" offset 1)
test_mod.chpl:14: thrown here
test_mod.chpl:8: uncaught here
```
I receive similar error messages to the above for reading in a list. I did not provide additional source code because it is essentially the same, but I can provide it if needed.
### `chpl --version`
```
chpl version 1.27.0
built with LLVM version 14.0.6
Copyright 2020-2022 Hewlett Packard Enterprise Development LP
Copyright 2004-2019 Cray Inc.
(See LICENSE file for more details)
```
### `printchplenv`
```
machine info: Darwin ethans-mbp.lan 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64
CHPL_HOME: /opt/homebrew/Cellar/chapel/1.27.0/libexec
script location: /opt/homebrew/Cellar/chapel/1.27.0/libexec/util/chplenv
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: llvm
CHPL_TARGET_ARCH: arm64
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: fifo
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: none
CHPL_HWLOC: none
CHPL_RE2: bundled
CHPL_LLVM: system
CHPL_AUX_FILESYS: none
```
Summary
Steps to Reproduce
Source Code:
Please note: Single quotes used here, but have also tried with \"
. Is this correct? It works when reading into a record.
var json_str = "{'key1': 'val1', 'key2': 'val2'}";
var newmem = openmem();
newmem.writer().write(json_str);
var nreader = newmem.reader();
var m: map(keyType=string, valType=string);
m.readThis(nreader);
Produces This Error
uncaught BadFormatError: bad format: missing expected literal (while reading ioLiteral "}" with path "unknown" offset 1)
test_mod.chpl:14: thrown here
test_mod.chpl:8: uncaught here
I receive similar error messages to the above for reading in a list. I did not provide additional source code because it is essentially the same, but I can provide it if needed.
chpl --version
chpl version 1.27.0
built with LLVM version 14.0.6
Copyright 2020-2022 Hewlett Packard Enterprise Development LP
Copyright 2004-2019 Cray Inc.
(See LICENSE file for more details)
printchplenv
machine info: Darwin ethans-mbp.lan 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64
CHPL_HOME: /opt/homebrew/Cellar/chapel/1.27.0/libexec
script location: /opt/homebrew/Cellar/chapel/1.27.0/libexec/util/chplenv
CHPL_TARGET_PLATFORM: darwin
CHPL_TARGET_COMPILER: llvm
CHPL_TARGET_ARCH: arm64
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: fifo
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: none
CHPL_HWLOC: none
CHPL_RE2: bundled
CHPL_LLVM: system
CHPL_AUX_FILESYS: none