-4

i've the following object and I need to put the "string",how should I put the value of name

{"name":{"_parent":["/test"]}}

inside,I try with " which doesnt work for me ,any idea ?

var file = {
   "name" : "{"name":{"_parent":["/test"]}}"

update I cannot use the jsonParse or stringify as I need to put it hardcoded

  • possible duplicate of [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – hindmost Aug 04 '15 at 14:15
  • Parsing goes from string to JS, folks. `file` is an object, not a string. – Dave Newton Aug 04 '15 at 14:16
  • Is your string will always be a safe JSON ? With JSON safe you may use this formula `"name": JSON.parse({"name": {"_parent": ["/test"]}})` – Saad Shahd Aug 04 '15 at 14:19
  • @SaadShahd No, that will give a parse error, because it's not a string. – Dave Newton Aug 04 '15 at 14:22
  • What does that mean, "you need to put it hardcoded"? What do you expect to see as the result? – Dave Newton Aug 04 '15 at 14:23
  • This won't even parse as written, btw, because you're using double-quotes everywhere. – Dave Newton Aug 04 '15 at 14:26
  • @DaveNewton - so how to fix the double-quats this is my questoin –  Aug 04 '15 at 14:27
  • Use single-quotes *and* double-quotes? You're not describing the problem or what you want very well. You didn't even include what you thought should work and didn't--but it'll work if you do it right. Whatever it is you're trying to do. – Dave Newton Aug 04 '15 at 14:27
  • @DaveNewton - how can you show me please –  Aug 04 '15 at 14:28
  • 3
    I have no clue what you're trying to do, so no. – Dave Newton Aug 04 '15 at 14:28
  • @DaveNewton - the value is coming from a file I want to put this value inside the object (name:= value),with the right escaping ...how? –  Aug 04 '15 at 14:30
  • @JhonDree *What specifically is coming from the file???* Are you trying to *read* a file and use that as the value?! In what JS environment? – Dave Newton Aug 04 '15 at 14:31

2 Answers2

0

Your question is a bit unclear but I think you want to do this The reason why JSON.parse() is not working for you is because you are missing the single quotes.

var yourString = '{"_parent":["/test"]}'

var file  = '{"name" : {"name":{"_parent":["/test"]}}}'

var obj = JSON.parse(file);
obj.name.name = yourString;
var backToString = JSON.stringify(obj);
Arno_Geismar
  • 2,296
  • 1
  • 15
  • 29
-2

Use this :

JSON.parse(file);

For more information you could look in this site :

http://www.w3schools.com/js/js_json.asp

OrcusZ
  • 3,555
  • 2
  • 31
  • 48