0

Is it possible to use a json string as a value of a json? Pass a valid json string as a value of a json element,

I need something like this sample:

{
"numberOfBlocks": 2,
"1": ""items": [
    {
        "Id": "111257",
        "Name": "AADbZyXqnVRCHjqWSpqQfgpu",
        "Order": 0
    },
    {
        "Id": "162972",
        "Name": "AADbZyXqnVRCHjqWSpqQfgpu",
        "Order": 1
    }]",
"2":""items": [
    {
        "Id": "111257",
        "Name": "AADbZyXqnVRCHjqWSpqQfgpu",
        "Order": 0
    },
    {
        "Id": "162972",
        "Name": "AADbZyXqnVRCHjqWSpqQfgpu",
        "Order": 1
    }]"

}

2
  • Is the value corresponding to 1 a String or an Object? Commented Apr 13, 2012 at 14:56
  • Can be a string or an Object... Commented Apr 13, 2012 at 17:43

3 Answers 3

1

Yes it is, here's a simple example:

{
   "a": 3,
   "b": "{ \"c\": \"hello\" } "
}

Or something more like what you have

{
  "numberOfBlocks": 2,
  "1": "{
    \"items\": [
      {
        \"Id\": \"111257\",
        \"Name\": \"AADbZyXqnVRCHjqWSpqQfgpu\",
        \"Order\": 0
      },
      {\
        \"Id\": \"162972\",
        \"Name\": \"AADbZyXqnVRCHjqWSpqQfgpu\",
        \"Order\": 1
      }]"
}

However, not that literal newlines aren't valid in JSON, so you'd need to escape the newlines too. In JS, it would look like the following

var a = {
  "numberOfBlocks": 2,
  "1": "{\
    \"items\": [\
      {\
        \"Id\": \"111257\",\
        \"Name\": \"AADbZyXqnVRCHjqWSpqQfgpu\",\
        \"Order\": 0\
      },\
      {\
        \"Id\": \"162972\",\
        \"Name\": \"AADbZyXqnVRCHjqWSpqQfgpu\",\
        \"Order\": 1\
      }]"
}

enter image description here

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

Comments

0

yes. However, I can see no reason to do that; it would need a lot of escaped quotes. And it makes parsing difficult, apart from that it will confuse everybody who looks at your source.

2 Comments

Agreed. If you are trying to do this then there is most likely a much nicer way of achieving whatever you are trying to do.
I have a very large json that takes too long to parse in ie7. I was trying to find a way to process it in smaller pieces. I know that I could have the server to split the data and make several requests to get the data, but it isn't an option right now...
0

Sure, you just need to escape double quotes as \" and newlines as \n. I don't recommend you do this, but it's certainly possible.

3 Comments

You need braces and quotes around what comes after "1":, i.e., "1": "{ \"items\" : [...] }"
Figured out that strings can't be multiline anyway, so I removed the example.
escaping all the characters may be too much, but is that or encode/decode with base64...

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.