0

Using the sample json response from jsonplaceholder.com, I would like to perform a Jolt transformation on the unnamed array that is returned.

However, using the jolt demo I have only been able to transform the array after naming the array ("records" in this example), and wrapping with curly braces. Like this:

Json Input:

{
"records": [
    {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz",
        "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
                "lat": "-37.3159",
                "lng": "81.1496"
            }
        },
        "phone": "1-770-736-8031 x56442",
        "website": "hildegard.org",
        "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
        }
    },
    {
        "id": 2,
        "name": "Ervin Howell",
        "username": "Antonette",
        "email": "Shanna@melissa.tv",
        "address": {
            "street": "Victor Plains",
            "suite": "Suite 879",
            "city": "Wisokyburgh",
            "zipcode": "90566-7771",
            "geo": {
                "lat": "-43.9509",
                "lng": "-34.4618"
            }
        },
        "phone": "010-692-6593 x09125",
        "website": "anastasia.net",
        "company": {
            "name": "Deckow-Crist",
            "catchPhrase": "Proactive didactic contingency",
            "bs": "synergize scalable supply-chains"
        }
    }
  ]
}

Jolt Spec:

[ { "operation": "shift", "spec": { "records": { "*": { "id": "records[&1].user-id", "username": "records[&1].user-username", "email": "records[&1].user-email", "address": { "street": "records[&2].user-street", "suite": "records[&2].user-suite", "city": "records[&2].user-city", "zipcode": "records[&2].user-zipcode" } } } } } ]

My example's goal is to flatten the object hierarchy returned in the response, while maintaining the basic [{}, {}, ...] structure.

How can I achieve this when the input is an unnamed json array?

wesmantooth
  • 625
  • 2
  • 11
  • 29

2 Answers2

1

I just realized the answer after posting...

Here is my revised spec for anyone that is interested:

[ { "operation": "shift", "spec": { "*": { "id": "[&1].user-id", "username": "[&1].user-username", "email": "[&1].user-email", "address": { "street": "[&2].user-street", "suite": "[&2].user-suite", "city": "[&2].user-city", "zipcode": "[&2].user-zipcode" } } } } ]

Simply match after declaring spec ("spec": { "*": { "), then access the unnamed array with [&1], [&2], etc

wesmantooth
  • 625
  • 2
  • 11
  • 29
1

below one works, but not flattens

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "id": "[&1].user-id",
          "username": "[&1].user-username",
          "email": "[&1].user-email",
          "address": {
            "street": "[&2].user-street",
            "suite": "[&2].user-suite",
            "city": "[&2].user-city",
            "zipcode": "[&2].user-zipcode"
          }
        }
      }
    }
  }
]
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47