0

I have a JSON object, and in the field resource_hours I want to store a JSON string.

{
  "id": 4,
  "resource": 1,
  "resource_hours": "json goes here",
  "start": "2009-10-10",
  "end": "2010-10-10",
  "created_at": "2017-06-01T13:23:06.103867Z",
  "modified_at": "2017-06-01T13:23:06.103867Z"
}

Here is the string/object I want to store:

{
  "winner": "john",
  "loser": "not john"
}

I tried this:

"resource_hours": "{"winner":"john","loser":"not john"}"

but obviously that raised an error due to the overlapping double quotes.

I also tried using \ to escape the quotes like so:

"resource_hours": "{\"winner\":\"john\",\"loser\":\"not john\"}"

This worked (no error was raised), but is it still a JSON string/object? Can it still be parsed as a JSON object if pulled from a database using an API?

1
  • 1
    var obj = JSON.parse(<yourstring>) and then obj.resource_hours = <value> Commented Jun 1, 2017 at 13:50

2 Answers 2

2

This will work for you using Python:

>>> import json
>>> j = {
... "id": 4,
... "resource": 1,
... "resource_hours": "json goes here",
... "start": "2009-10-10",
... "end": "2010-10-10",
... "created_at": "2017-06-01T13:23:06.103867Z",
... "modified_at": "2017-06-01T13:23:06.103867Z"
... }
>>> j
{'id': 4, 'resource': 1, 'resource_hours': 'json goes here', 'start': '2009-10-10', 'end': '2010-10-10', 'created_at': '
2017-06-01T13:23:06.103867Z', 'modified_at': '2017-06-01T13:23:06.103867Z'}

>>> # converting your data to json
>>> act_json = json.dumps(j)
>>> act_json
'{"id": 4, "resource": 1, "resource_hours": "json goes here", "start": "2009-10-10", "end": "2010-10-10", "created_at":
"2017-06-01T13:23:06.103867Z", "modified_at": "2017-06-01T13:23:06.103867Z"}'

>>> # value that needs to be added
>>> val_to_store = {
... "winner": "john",
... "loser": "not john"
... }

>>> # getting the data and converting it to dictionary
>>> j = json.loads(act_json)

>>> # assigning the values
>>> j['resource_hours'] = val_to_store
>>> j
{'id': 4, 'resource': 1, 'resource_hours': {'winner': 'john', 'loser': 'not john'}, 'start': '2009-10-10', 'end': '2010-
10-10', 'created_at': '2017-06-01T13:23:06.103867Z', 'modified_at': '2017-06-01T13:23:06.103867Z'}

>>> # converting back to json, if needed
>>> to_json = json.dumps(j)
>>> to_json
'{"id": 4, "resource": 1, "resource_hours": {"winner": "john", "loser": "not john"}, "start": "2009-10-10", "end": "2010
-10-10", "created_at": "2017-06-01T13:23:06.103867Z", "modified_at": "2017-06-01T13:23:06.103867Z"}'

Happy coding !!!

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

1 Comment

Thanks! Good Python solution.
1

JSON.stringify method allows you to convert object into a string:

const objectWillBeStoredInJson = {
  "winner": "john",
  "loser": "not john"
}

const object = {
  "id": 4,
  "resource": 1,
  "resource_hours": JSON.stringify(objectWillBeStoredInJson),
  "start": "2009-10-10",
  "end": "2010-10-10",
  "created_at": "2017-06-01T13:23:06.103867Z",
  "modified_at": "2017-06-01T13:23:06.103867Z"
}

Now, object.resource_hours has a string that refers to objectWillBeStoredInJson when parsed.

You can use JSON.parse when you want to access it:

const objectWillBeStoredInJson = JSON.parse(object.resource_hours)

1 Comment

Thank you for the Javascript solution!

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.