DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Mo Boudra
Mo Boudra

Posted on • Updated 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 Boudra Author • Edited on

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

Collapse
thulasiram_ponnam_8d5e32b profile image
!2C RAM @ jsonformatter.dev

try & instantly validate and Beautify JSON using jsonformatter.dev

🌚 Life is too short to browse without dark mode