1

I have an unusual problem to solve here. I have an array of Guids

[
 "c01f8237-72c8-4fa6-9c53-1915750385aa",
 "2c8a471b-c408-436c-81b1-3f3867d8ffb4",
 "27a44d46-12bd-4784-ceed-57ada31b0e33"
] 

This array has to be transformed into:

   {
     id: "c01f8237-72c8-4fa6-9c53-1915750385aa",
     id: "2c8a471b-c408-436c-81b1-3f3867d8ffb4",
     id: "27a44d46-12bd-4784-ceed-57ada31b0e33"
   } 

I know that shouldn't be done, but unfortunately cannot control the back end part. Any idea?

Thanks

6
  • 3
    Same key cannot be there in the object more than once Commented Apr 14, 2014 at 16:28
  • I know that very well. But as I said in the question, unfortunately the back end that receives the object hasn't been built in a standard way and I cannot change it. Commented Apr 14, 2014 at 16:30
  • 2
    @Mauro74 what he means is that it is not possible in javascript. However, what exactly is your back end expecting? Backends expect serialization formats, not javascript. The above is not valid JSON, is not not expecting json? Commented Apr 14, 2014 at 16:33
  • 2
    So you don't want it transformed into an object, but rather serialized into a string in that form. Seems like a very simple task if that's the case. What's the problem? Commented Apr 14, 2014 at 16:34
  • 1
    It sounds like the back end is not really expecting "JSON", but is really just using something trivial like String.split() to build an array that it then loops over. If so, you're stuck using string concatenation to build the input. Commented Apr 14, 2014 at 16:36

2 Answers 2

2

The whole point of a dictionary key is that it uniquely maps to some value. Your desired output attempts to duplicate a key and therefore is neither possible nor does it make sense.

If you're passing this to a backend (as you suggest), then of course you can manually build a string to pass over the wire that duplicates keys in the payload, but you won't be able to actually do it in JavaScript first. You'll have to manually build the string.

Also note that you can call this format whatever you want, but you can't call it JSON and you can't use JSON libraries to build it (because it's not JSON). If your API expects a custom format, then you need to write code to build that custom format.

Sign up to request clarification or add additional context in comments.

2 Comments

I know that very well. But as I said in the question, unfortunately the back end that receives the object hasn't been built in a standard way and I cannot change it.
If you're passing this to a backend, then of course you can manually build a string to pass over the wire that duplicates keys in the JSON payload, but you won't be able to actually do it in JavaScript first.
2

If all you want is a string just do something like

var str = '{' + idArray.map(function(id) { 
       return "id: "+id
   }).join(',\n')+'}';

I have no idea what mime type you would put on that though since its not valid JSON.

2 Comments

Or just: str = '{id:"' + idArray.join('", id:"') + '"}' Probably just text/plain MIME. And you're missing the quotes around the IDs in the result.
This answers the question, as non-sensical as it may be

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.