0

I want to add a new key-value pair in JSON object with nunjucks template, but unable to add it(don't want to add any custom filter).

suppose I have an object

var obj = {"id": "1", "name": "test", "child": {"id": "2", "status": "true"}, 
           "template": "{{obj.child}}"}

Now I want to add a new key like

{"template": "{% set json = obj.child %}{% set json.name = 'testing' %}"}

So the final output as I want

{
  "id": "1",
  "name": "test",
  "child": {
    "id": "2",
    "status": "true"
  },
  "template": {
    "id": "2",
    "status": "true",
    "name": "testing"
  }
}

Please let me how can I do?

  • What does `"template": "{{obj.child}}"` mean? – Suhail Gupta Sep 15 '17 at 12:20
  • It mean template will access current json object's(obj) key ('child') after render – Vikas Sharma Sep 15 '17 at 12:27
  • 1
    `{% set obj.name = 'testing' %}` I think that it's not possible without custom filter/global function. See [**parseSet**](https://github.com/mozilla/nunjucks/blob/master/src/parser.js). As I understand it: supports only `{% set myvar = smth %}` and `{% set myvar, myvar2 = smth %}`. – Aikon Mogwai Sep 17 '17 at 00:18

0 Answers0