Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON data into a variable assignment format

Tags:

json

shell

jq

I am trying to parse JSON data into variable format

[
  {
    "Name" : "a",
    "Value" : "1"
  },
  {
    "Name" : "b",
    "Value" : "2"
  },
  {
    "Name" : "c",
    "Value" : "3"
  }
]

output should be like

a=1
b=2
c=3

This is what I tried, but it is not giving the expected result:

jq '.[].Value' file.txt 
like image 701
Sandeep Sharma Avatar asked Sep 17 '26 21:09

Sandeep Sharma


2 Answers

Since you're only printing out two values, it might just be easier to print out the strings directly.

$ jq -r '.[] | "\(.Name)=\(.Value)"' file.txt
like image 128
Jeff Mercado Avatar answered Sep 20 '26 15:09

Jeff Mercado


You can use the following jq command:

jq -r '.[]|[.Name,.Value]|join("=")' file.json

Output:

a=1
b=2
c=3
like image 29
hek2mgl Avatar answered Sep 20 '26 16:09

hek2mgl