String Array to JSON String

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

  1. Add { to beginning of the output string
  2. Iterate over all entries, adding them to the output string, delimited by ,
  3. Add } to the end of the output string
  4. 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}'

r2d2292

Posted 2018-06-11T02:07:30.443

Reputation: 63

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.950

Whats 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.477

The 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

Answers

0

Retina, 23 bytes

%0`\w+
"$&"
¶
,
.+
{$&}

Try it online! Explanation:

%0`\w+
"$&"

Quote all of the keys.

¶
,

Join the array elements together.

.+
{$&}

Wrap the result in brackets. This can also be done using the following code for the same byte count:

^
{
$
}

Neil

Posted 2018-06-11T02:07:30.443

Reputation: 95 035

0

C 108

Thanks to Jonathan Frech this answer is now incredibly shorter
Thanks to @ceilingcat for some very nice pieces of golfing - now even shorter

i;main(c,v)char**v;{for(;++i<c;printf("%c\"%s\":%s",!~-i*79+44,v[i],++*v))*(*v=index(i[v],58))=0;puts("}");}

Try it online!

Jerry Jeremiah

Posted 2018-06-11T02:07:30.443

Reputation: 1 217

113 bytes, keeping with using a main function. – Jonathan Frech – 2018-06-11T16:57:08.943

112 bytes. – Jonathan Frech – 2018-06-11T21:09:14.700

@JonathanFrech I would never have come up with !~-i++*79+44 That's not just golfed better - it's a better answer. Do you want to post it separately or shall i edit my answer? – Jerry Jeremiah – 2018-06-12T05:18:59.753

Regarding the golf: (i==1?"{":",") <-> (i==1?'{':',') (when using %c instead of %s) <-> (i==1?123:44) (substituting actual ASCII values) <-> 44+(123-44)*(i==1) <-> 44+79*(i==1) <-> 44+79*!(i!=1) <-> 44+79*!(i-1) <-> 44+79*!~-i <-> !~-i*79+44. The post-increment was used with regards to the entire program (not locally explainable). Regarding the posting: feel free to use the golfs in your post. – Jonathan Frech – 2018-06-12T16:57:03.607

0

Python 3, 59 bytes

lambda x:"{%s}"%",".join('"'+a.replace(":",'":')for a in x)

Try it online!

-6 bytes thanks to ovs

HyperNeutrino

Posted 2018-06-11T02:07:30.443

Reputation: 26 575

59 bytes using a.replace(':','":') – ovs – 2018-06-11T17:13:00.800

@ovs oh thanks :D – HyperNeutrino – 2018-06-12T02:06:17.297

0

JavaScript (Node.js), 35 bytes

a=>JSON.stringify(eval(`({${a}})`))

Try it online! 43 bytes without builtins:

a=>`{${a.map(s=>s.replace(/\w+/,`"$&"`))}}`

Try it online!

Neil

Posted 2018-06-11T02:07:30.443

Reputation: 95 035

0

Lua, 122 bytes

loadstring[[s="{"for i,v in ipairs(...)do x,y=v:match("(.-):(.+)")s=s.."\""..x.."\":"..y..","end return s:sub(1,-2).."}"]]

Try it online!


This is an anonymous function. It technically doesn't "print out JSON string [...]", but instead it returns this string, so that when calling it you could print. I did that because I needed to first convert the input from string to an actual list, and I did that on the argument of the function call.

If this makes my answer invalid I'll move the string to table (list) "parser" from the footer to the actual code. But I don't think it should, because the input shown as a table, so the code that takes a string and transforms it into a table shouldn't count.

Visckmart

Posted 2018-06-11T02:07:30.443

Reputation: 151