Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

Convert key = value pairs to JSON

$
0
0

I am trying to convert a file containing key = value pairs into JSON.This file might contain Windows EOL (\r\n) and empty lines.

Given the following input (mind the empty lines):

foo = aabar = bbqux = ccwhite space = white space* = special-char

This is the expected result:

{"foo": "aa","bar": "bb","qux": "cc","white space": "white space","*": "special-char"}

I managed to go this far:

{"foo": "aa"}{"bar": "bb"}{"qux": "cc"}{"white space": "white space"}{"*": "special-char"}

Using the following command:

 jq --raw-input 'split("\n") | map(split(" = ") | { (.[0]): .[1] }) | .[]'

But I can not figure out the missing bit. What is missing or is this a better way to achieve this?

Edit: added constraint about empty line and Windows EOL


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>