0

I have a JSON which looks like this:

{
    "lorem": "ipsum",
    "dolor": "sid",
    "data": {
        "key1": "value1",
        "key2": "value2"
    }
}

and I want an output which is ini like where I only need the content of 'data' (which is always flat, no branches). The output should look like this:

key1=value1
key2=value2

I can use jq (just don't get it running) but have to use a bash script for it. Can anyone help?

Iarwa1n
  • 460
  • 3
  • 12

3 Answers3

3

jq solution:

jq -r '.data | to_entries[] | "\(.key)=\(.value)"' input.json

The output:

key1=value1
key2=value2
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

This will work in BASH.

#!/bin/bash

all_keys=$( cat input.txt );

while read key
do
    grep "$key" ./text.txt | awk -F':' '{ print $1"="$2}' | tr -d '[", ]'
done <<< "$all_keys"

Assuming you values are in text.txt and that you have your keys in input.txt.

Regards!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
0

In python This reads stdin and outputs the desired "key=value" one per line

#!/usr/bin/python

import json
import sys

data = sys.stdin.read()

json_structure=json.loads(data)

start_point=json_structure["data"]

for k in start_point.keys():
    print("%s=%s" % (k, start_point[k]))

If I was using python to unmangle the json input however, I would probably rewrite the bash script in python

Vorsprung
  • 32,923
  • 5
  • 39
  • 63