1

I have a JSON like this

{
  "id": "1234",
  "name": "something",
  "list": [
    {
      "A": "Something"
    },
    {
      "B": "Something1"
    }
  ]
}

What I would like is to do is add id and name to both the JSON's present inside list I've gone through a couple of questions and I couldn't find anywhere where somebody had done this.

2 Answers 2

4

I believe the following Shift spec will work:

{
    "id|name": "&",
    "list": {
      "*": {
        "@(2,id)": "&2.[&1].id",
        "@(2,name)": "&2.[&1].name",
        "*": "&2.[&1].&"
      }
    }
}

With your sample data, the output produced was:

{
    "id": "1234",
    "name": "something",
    "list": [{
        "id": "1234",
        "name": "something",
        "A": "Something"
    }, {
        "id": "1234",
        "name": "something",
        "B": "Something1"
    }]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I had already figured it out, this is what I did. Thanks a lot!
1

This spec should give you what you want:

[
  {
    "operation": "shift",
    "spec": {
      "list": {
        "*": {
          "@(2,id)": "&2.[&1].id",
          "@(2,name)": "&2.[&1].name",
          "*": "&2.[&1].&"
        }
      }
    }
  }
]

With your input it gives the following as output:

{
  "list" : [ {
    "A" : "Something",
    "id" : "1234",
    "name" : "something"
  }, {
    "B" : "Something1",
    "id" : "1234",
    "name" : "something"
  } ]
}

1 Comment

Oops didn't see James had answered already, sorry about that! Looks like you can choose between the two depending on whether you want the original top-level fields in the outgoing JSON, although you can just remove his id|name entry and get what I had. Upvote his, I should've refreshed the page before submitting :)

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.