DEV Community

Mo
Mo

Posted on Edited on

Convert JSON to CSV using JSON path expressions with Konbert

JSON paths are a very simple and intuitive way to reference elements in JSON documents, similar to CSS selectors, but for JSON.

This is how it works, if you had a document like this:

[
  {
    "name": "Christopher",
    "age": "23",
  },
  {
    "name": "Michael",
    "age": "45",
  }
]
Enter fullscreen mode Exit fullscreen mode

You could extract the names of all the person records with this expression:

$.*.name
Enter fullscreen mode Exit fullscreen mode

where:

  • $ references the root element, in this case an array
  • * is to repeat the expression, so it will match on each object of the array
  • name is to get the name property of the object

So if you used this expression to convert to CSV, this would result in:

name
Christopher
Michael
Enter fullscreen mode Exit fullscreen mode

This is very useful when you need to flatten very complex JSON documents, and you only want to extract a few fields.

Go ahead and try our custom JSON to CSV conversion tool. Any feedback is welcome!

Top comments (3)

Collapse
 
alelazcano profile image
Alejandro Lazcano •

sqlify.io
is for sale.

LOL.

Collapse
 
moboudra profile image
Mo • • Edited

Hi Alejandro, Sqlify is alive and kicking, we just rebranded to konbert.com/
:)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.