Retrieve array values to objects with key values from a jsonarray

Written by:

To truly flatten (or explode) the array so that each author gets their own independent object, you need to change the order of operations. You need to iterate over the authors first, and then reach “back up” to the book for the title and ISBN.

This example is using the example json from: https://try.jsonata.org/EQzHL9xpZ

library.books.(
  $book := $;
  authors.{
    "author": $,
    "title": $book.title,
    "isbn": $book.isbn,
    "price": $book.price
  }
)

This wil give this result:

[
  {
    "author": "Abelson",
    "title": "Structure and Interpretation of Computer Programs",
    "isbn": "9780262510875",
    "price": 38.9
  },
  {
    "author": "Sussman",
    "title": "Structure and Interpretation of Computer Programs",
    "isbn": "9780262510875",
    "price": 38.9
  },
  {
    "author": "Kernighan",
    "title": "The C Programming Language",
    "isbn": "9780131103627",
    "price": 33.59
  },
  {
    "author": "Richie",
    "title": "The C Programming Language",
    "isbn": "9780131103627",
    "price": 33.59
  },
  {
    "author": "Aho",
    "title": "The AWK Programming Language",
    "isbn": "9780201079814"
  },
  {
    "author": "Kernighan",
    "title": "The AWK Programming Language",
    "isbn": "9780201079814"
  },
  {
    "author": "Weinberger",
    "title": "The AWK Programming Language",
    "isbn": "9780201079814"
  },
  {
    "author": "Aho",
    "title": "Compilers: Principles, Techniques, and Tools",
    "isbn": "9780201100884",
    "price": 23.38
  },
  {
    "author": "Lam",
    "title": "Compilers: Principles, Techniques, and Tools",
    "isbn": "9780201100884",
    "price": 23.38
  },
  {
    "author": "Sethi",
    "title": "Compilers: Principles, Techniques, and Tools",
    "isbn": "9780201100884",
    "price": 23.38
  },
  {
    "author": "Ullman",
    "title": "Compilers: Principles, Techniques, and Tools",
    "isbn": "9780201100884",
    "price": 23.38
  }
]

see it yourself here: https://try.jsonata.org/EQzHL9xpZ


Discover more from Node-RED LoRaWAN CouchDB and more

Subscribe to get the latest posts sent to your email.

Leave a comment