简体   繁体   中英

Parsing JSON using jq or Python

I have this nested JSON

[
  "[[Input=[Name=ABC, createDateTime=2019-30-11, RollNumber=9]]]",
  "[[SubjectList=[Summer=, Winter=, Autumn=, Spring=, rList=, sList=, additionalList=, emailList=, FoodList=, sAssignmentList=, summerworkList=, outdoorList=, movielist=]]]",
  "[ProcessingDate=2018-10-06]",
  "[Hobbies=Football]",
  "[Phone=Android,,]"
]

How can I process this JSON and get the value football or rollnumber using Python?

This is what I tried:

Code

import json
row = '''[
  "[[Input=[Name=ABC, createDateTime=2019-30-11, RollNumber=9]]]",
  "[[SubjectList=[Summer=, Winter=, Autumn=, Spring=, rList=, sList=, additionalList=, emailList=, FoodList=, sAssignmentList=, summerworkList=, outdoorList=, movielist=]]]",
  "[ProcessingDate=2018-10-06]",
  "[Hobbies=Football]",
  "[Phone=Android,,]"
]'''
row_dict = json.loads(row)
print(row_dict[3])

Using this - I get following output:

[Hobbies=Football]

But I am missing next level parsing to get just football as output

Here is an approach that uses capture on the non-json strings in the array.
It assumes the [:alnum:] posix regex character class suffices to match the values after the =
Sample execution assuming data in test.json

$ jq -M '.[] | capture("Hobbies=(?<Hobbies>[[:alnum:]]+)")' test.json
{
  "Hobbies": "Football"
}

Here is a variation which produces exactly Football :

$ jq -Mr '.[] | capture("Hobbies=(?<Hobbies>[[:alnum:]]+)") | .Hobbies' test.json
Football

Here's an example script which uses multiple captures and combines them with add

[ .[]
  |  capture("Hobbies=(?<Hobbies>[[:alnum:]]+)")
   , capture("RollNumber=(?<RollNumber>[[:alnum:]]+)")
] | add

Sample execution assuming script in test.jq

$ jq -M -f test.jq test.json
{
  "RollNumber": "9",
  "Hobbies": "Football"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM