1
Introduction
I've seen throughout the code-golf challenges several simple tasks involving JSON, such as interpreting JSON strings with RegExs, and almost every other manipulation I could think of.
In a binding for AppleScript that I was using for a Node project, I noticed that objects would be returned in a strange way: there would be an array of properties in string form, instead of a JSON object being returned as expected. This is where the challenge comes in...
Algorithm
- Add
{
to beginning of the output string - Iterate over all entries, adding them to the output string, delimited by
,
- Add
}
to the end of the output string - Print out the output string
Challenge
Make this strange return format usable. Convert an array of JSON 'key:value'
s (in string form) to a full JSON object string.
Rules
- Any language that can do string manipulations allowed
- Must print out JSON string with surrounding curly braces
{}
, and double quotes""
around string values AND all properties ("property":"value"
or"property":3.14
, etc.) - Must return a valid, parseable JSON string
- Lowest byte count wins per language
Example Input and Output
Input:
['prop1:"val1"', 'prop2:12', 'prop3:3.14', 'prop4:false']
Output:
'{"prop1":"val1", "prop2":12, "prop3":3.14, "prop4":false}'
1Welcome to ppcg.SE! Hope you have a great time here. – Leaky Nun – 2018-06-11T02:10:06.297
1We don't put languages in boxes ^^ – Leaky Nun – 2018-06-11T02:10:12.637
2This challenge seems to be a [tag:code-golf] challenge, but it doesn't say in the post. Could you make it more clear? – JungHwan Min – 2018-06-11T02:11:52.797
This needs a tag for a challenge type. I (like JungHwanMin) am assuming code golf but it really needs a tag. – Jerry Jeremiah – 2018-06-11T02:24:12.870
1Hello, and welcome to the site. I'm voting to close this as unclear. There are a couple of things I find unclear. The first is that terms like "golfing" and "functional" languages are unclear, there are tons of edge cases. I'd recommend getting rid of the bonuses altogether, which would make this challenge clearer and better. It's also not clear to me exactly what the process is to change the strings to json is. I think it ought to be defined much more concretely. – Post Rock Garf Hunter – 2018-06-11T02:39:57.183
1Your test case does not seem to match your description. In the test case it seems that quotations were placed around attribute names, but that is not apparent in your specification. – Post Rock Garf Hunter – 2018-06-11T03:05:35.177
Suggested test-cases: a key containing
:
, a value containing:
, same for,
. – Nit – 2018-06-11T22:20:15.950Whats the actual input format? A list of strings? What characters can be in the string? ie, can the value or key contain a
:
or"
? – Jo King – 2018-06-12T07:14:55.477The input format is either a string (or cstring, if unsupported) single-dimensional array. The input will only involve ASCII characters. The value can contain
"
(as\"
) and:
, but the property can't, as the property isn't a string in the input format (being in the format'property:"value"'
for strings in an entry), so it wouldn't support string escape sequences. – r2d2292 – 2018-06-14T00:13:44.013